Negative appearance capture

Suppose I have the following question:

abcdef ghidef 

and I want to combine a word ending in def rather than abc (in this case it will be ghidef). How can I match this?

When i use:

 (?<!abc)def 

I get a second def , but I don't get ghi here.

+5
source share
1 answer

No need to search. You can use with a negative view:

 \b(?!abc)\w*def\b 

RegEx Demo

RegEx Distribution:

  • \b - affirm word boundary
  • (?!abc) - the negative look that claims the word does not start with abc after \b (word boundary)
  • \w* - matches 0 or more word characters
  • def - the final text of the word def
  • \b - word boundary
+3
source

Source: https://habr.com/ru/post/1265888/


All Articles