Home > Blockchain >  How to not match text if it is followed by a specific text?
How to not match text if it is followed by a specific text?

Time:11-13

https://regex101.com/r/jiRO1M/1

Pattern: A.*?D(?!toA.*?D)

Here is the text

AzzDtoAxxD
AvvD
AnnD

I want to match AvvD and AnnD only

CodePudding user response:

There are a couple exotic ways to solve this.
Depends on what your dependency is.

This method insures not a pair of A.*?D separated by a to.

(?m)^(?!.*?A.*?DtoA.*?D).*?(A.*?D)

https://regex101.com/r/HeH2bD/1

 (?m)
 ^ 
 (?! .*? A .*? DtoA .*? D )
 .*? 
 ( A .*? D )                   # (1)

CodePudding user response:

Use

\bA[^AD]*D\b

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  A                        'A'
--------------------------------------------------------------------------------
  [^AD]*                   any character except: 'A', 'D' (0 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  D                        'D'
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char

OR:

\bA[^AD]*D(?!toA.*D)

See this regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  A                        'A'
--------------------------------------------------------------------------------
  [^AD]*                   any character except: 'A', 'D' (0 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  D                        'D'
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    toA                      'toA'
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    D                        'D'
--------------------------------------------------------------------------------
  )                        end of look-ahead
  • Related