0

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.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
ARHassing
  • 23
  • 4
  • Is this for PCRE? Please mention tool/lang you're using. In PCRE you can exclude the part by using [`(*SKIP)(*F)` technique](https://stackoverflow.com/questions/24534782/how-do-skip-or-f-work-on-regex). Eg for your task something like: [`\b222\d.*(*SKIP)(*F)|3\d{4}`](https://regex101.com/r/tGTxp6/1) – bobble bubble Jul 07 '22 at 12:49

1 Answers1

1

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
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360