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?
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}
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With