Friday, November 12, 2021

[SOLVED] Return dash followed by a single character

Issue

This works as expected:

([^\u0000-\u007F])+-हा([^\u0000-\u007F])+

Returns:

ब-हाणपूर
ब-हाणी
बनियन-हाफ

But I am looking for 1 character followed by dash. The expected output is:

ब-हाणपूर
ब-हाणी

I tried to replace + sign with character count like this...

([^\u0000-\u007F]){1}-हा([^\u0000-\u007F])+

But it returned the same 3 results. How do I return the first 2?


Solution

You need anchors:

^([^\u0000-\u007F])-हा([^\u0000-\u007F])+$

Demo


You asked 'What if I need 5 characters to the left of dash?'

The regex portion [^\u0000-\u007F] as written matches a single character that meets that criterion. If you want more or less than one, use a regex quantifier to describe how many you want.

In this case, if you want 5, you would use:

^([^\u0000-\u007F]{5})-हा([^\u0000-\u007F])+$


Answered By - dawg