Home > Mobile >  Why doesn't lookahead match on either side of the following term?
Why doesn't lookahead match on either side of the following term?

Time:12-31

I'm experimenting with using lookaheads for checking if a string contains a pattern at any position, however, I'm struggling a bit to understand it.

The pattern (?=.*px)[\d] should match (to my understanding) any digits (as groups, therefore 12 would be one match) that also have either before or after the letters p and x, in that order, regardless of the position (.*).

That pattern works as expected while using "12px", matching the 12 but not the px. However, if I use px12 it doesn't match the 12 anymore and I'm struggling to understand why. Also, the string "12pxpx12px12" matches the first and second 12 as both have px after it, but not the last one.

Here are some examples for reference:
Regex example

I'd appreciate it if someone could explain to me what is wrong in terms of what I'm looking forward to achieving and how the proper way of writing the regex would be.

CodePudding user response:

A lookahead (?=px) means "the substring immediately following this point is px", and \d means "the substring at this point is multiple digits". Those are mutually incompatible statements. Lookarounds are zero-width assertions, meaning that if you wanted to match something before a concrete assertion like \d , you can't use lookahead, you need to use lookbehind.

Off the top of my head, the best solution I can think of uses exclusive-or:

(?<=px)\d |\d (?=px)

Try it on regex101

  • Related