Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex: Lookaround Before/After the Match

consider this text: 100 dollars

If I want to match 100 (using lookahead), I wrote this:

\d{3}(?= dollars)

and as far as I know, that pattern means: find 3 digits only when(if) it is followed by " dollars"

but lookahead can be used in an odd way; again matching 100 in the above mentioned text:

(?=\d{3} dollars).{3}

How is it possible? How do we interpret this second use of lookahead?

like image 200
wiki Avatar asked Oct 31 '22 22:10

wiki


1 Answers

Remeber that lookarounds are zero-width assertions. Meaning that they don't consume characters as they are matching. They are basically a check from a given point in the string. In the second regex the engine first checks whether from a specific point in the string the pattern inside the lookaround matches and if so, the matching continues from that location this time by consuming characters (.{3}).

like image 79
Farhad Alizadeh Noori Avatar answered Nov 15 '22 11:11

Farhad Alizadeh Noori