Home > Blockchain >  Regex pattern immediately followed by another String
Regex pattern immediately followed by another String

Time:05-25

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 match
  • ab Match literally
  • \w* Match optional word chars
  • \W* Match optional non word chars
  • \sand Match a whitespace char followed by and
  • \b A word boundary

Regex demo

CodePudding user response:

As simple as abcd\W and.

Or abcd(?=\W and) if you only want to match the textual part.

  • Related