How to find string(conaining special char) from regular expression with boundary condition

36 views Asked by At

Below is my string from which i want to find substring:

pull if {{MySQL}}.EmployeePerson!.City is 'Seattle' then {{MySQL}}.EmployeePerson!.StateProvinceID else '0' as StateProvinceID from {{MySQL}}.EmployeePerson!

and here is my regular expression

EmployeePerson!\b

so i need to find "EmployeePerson!" string but i am not able to get any result , so please help.

Thanks in advance.

1

There are 1 answers

2
Wiktor Stribiżew On

When you put \b after !, you require a word char after !.

See what word boundary matches:

There are three different positions that qualify as word boundaries:

  • Before the first character in the string, if the first character is a word character.
  • After the last character in the string, if the last character is a word character.
  • Between two characters in the string, where one is a word character and the other is not a word character.

Thus, you need to remove \b if you do not need to check the context after !, or replace \b with \B that will require a non-word char or end of string.

To find EmployeePerson! as a whole word, just use

\bEmployeePerson!

or

\bEmployeePerson!\B

or - if you need to make sure there are spaces or start/end of string on both ends:

(?<!\s)EmployeePerson!(?!\s)

or - if you want to make sure there are no words chars on both ends:

(?<!\w)EmployeePerson!(?!\w)