Search code examples
phpregexpcre

how to not capture entire group with regex negative lookahead


I have numbers like 1556278013708 or 34566 in text. Some of them followed by ", eg 1556278013708"

I don't need numbers followed by ", so I tried (\d+)(?!"), but this still matches those numbers, just without the final digit, eg 155627801370

How to make to not capture entire numbers with " right after?

Eg, 34567 - Match all of it

Eg, 34567" - Match none of it


Solution

  • You can make the quantifier possessive instead - that way, once the engine matches all the digits, once it sees that the negative lookahead fails, the whole match will fail completely, rather than trying to backtrack:

    \d++(?!")
    

    https://regex101.com/r/6bIqdI/1

    (note that if the whole match is those digits, there's no need for a capture group, just extract the whole match)