Home > Blockchain >  Regex: negative lookbehind with escaped asterisk not working right
Regex: negative lookbehind with escaped asterisk not working right

Time:12-08

I have the following data:

FIT 13.5 ON 16 LNR, LWD[GR,RES,PWD] @ 10340, M12.8, NO SWC, *13-3/8 X 16* (13.9) EXP LNR @ 8696-10340, FIT 14.4, LWD(GR, RES, PWD  
FIT 12.4 ON 20, LWD[ARC,PWD  
FIT 15.0 ON 16, LWD[ARC,PWD]  
FIT 13.3 ON 11-3/4, LWD[ARC,SON,PWD] @ 12065, PREP TO DRILL  
9-5/8 LNR @ 11695-16163, FIT 14.9, LWD[ARC,SON,DEN,NEUT,CMR,TESTRAK], (SON FAILED), NO SWC, REAMING (TO RUN CSG ) 

I'm trying to write a Regex which will find all fractions without an asterisk, so I used a negative lookbehind:

(?<=\*)(\d{1,2}-\d\/\d)

Naturally (because nothing can be easy), Regex thinks the 1 in the 13 is part of the lookbehind:

Regex negative lookbehind

If I change it to a positive lookbehind, it works as it should if I wanted to find fractions with an asterisk:

Regex positive lookbehind

That's not what I need. I'm trying to get Regex to ignore all figures between two asterisks in a string.

Any idea on how to make it do what I need?

Thanks!

CodePudding user response:

The fourth bird's suggestion of adding a word boundary seems to have done the trick:

enter image description here

Thank you so much for your help!

CodePudding user response:

About

Regex thinks the 1 in the 13 is part of the lookbehind

The 1 is not part of the lookbehind, but the assertion (?<=\*) is true when there is no asterix directly to the left.

That assertion is true at a lot of positions, only after *1 it can match the pattern (\d{1,2}-\d\/\d) that is why you get a match without the leading 1.

You can omit the capture group to get a match only, and add a leading word boundary \b to prevent the lookbehind to fire at every position scanning for a match, only when it encounters a word boundary in the string.

Note that you don't have to escape the forward slash, if that is not the delimiter of the pattern.

\b(?<!\*)\d{1,2}-\d/\d

Regex demo

  • Related