wiki
wiki

Reputation: 2033

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?

Upvotes: 5

Views: 1148

Answers (1)

Farhad Alizadeh Noori
Farhad Alizadeh Noori

Reputation: 2306

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}).

Upvotes: 2

Related Questions