简体   繁体   中英

Capture with negative look behind

Let's assume I have the following subject:

abcdef ghidef

and I want to match word ending with def not preceeded by abc (in this case it would be ghidef). How can I match this?

When I use:

(?<!abc)def

I'm getting the 2nd def but I'm not getting ghi here.

No need of lookbehind. You can use with a negative lookahead:

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

RegEx Demo

RegEx Breakup:

  • \\b - assert a word boundary
  • (?!abc) - is negative lookahead that asserts a word doesn't start with abc after \\b (word boundary)
  • \\w* - match 0 or more word characters
  • def - ending text of word is def
  • \\b - word boundary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM