In the following input string
abcd, of regex is not my cup of tea and coffee , but abcd – and efgh of JS are my whisky
I want to match abcd - and only.
More generally ab.*? followed by any number of special characters and spaces which then followed with literal and
I tried the following pattern abc.*?(?!(\w))\sand but this is matching both strings highlighted bold in the input string.
>Solution :
The pattern abc.*?(?!(\w))\sand matches too much, as .*? can backtrack (it matches any character) till this assertion (?!(\w)) it true and it can match \sand
But it is the same as writing abc.*?\sand because this part is always true (?!(\w))\s because the next character is \s and therefore automatically not \w
You could use:
\bab\w*\W*\sand\b
The pattern matches:
\bA word boundary to prevent a partial word matchabMatch literally\w*Match optional word chars\W*Match optional non word chars\sandMatch a whitespace char followed byand\bA word boundary