ARHassing
ARHassing

Reputation: 23

REGEX: Exclude matches after a specific string

So I am trying to get the token that starts with a 3.

The AAXX line below is picking it up right. However, doing some edge cases, the wrong 3 token is getting picked up in the BBXX line. The difference is that in BBXX line, the 3 group appears after a 222\d{2} token.

This 222\d{2} token is optional. I will only want the 3 token that appears before it, never after it. And even then, the 3 token is optional and might not appear.

So basically, want the optional token that begins with 3 that either appears before 222\d{2} if it exists, or before the end of the line if it doesn't.

I don't know if that requires a negative look behind, or just how to exclude a match that would start with 222\d{2}.

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

Thanks.

Upvotes: 0

Views: 101

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522762

This answer assumes that you can accept using a capture group for the 333 number to be matched. We can use a negative lookahead with tempered dot here:

^((?!\b222\d+).)*(3\d{4})

Demo

This pattern says to match:

^                 from the start of the line
((?!\b222\d+).)*  match all content WITHOUT crossing over a 222x number
(3\d{4})          match and capture in \1 a 3x number

Upvotes: 1

Related Questions