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.
CodePudding user response:
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:
\b
A word boundary to prevent a partial word matchab
Match literally\w*
Match optional word chars\W*
Match optional non word chars\sand
Match a whitespace char followed byand
\b
A word boundary
CodePudding user response:
As simple as abcd\W and
.
Or abcd(?=\W and)
if you only want to match the textual part.