Search code examples
regexregex-lookarounds

Regex pattern reads correctly but doesn't produce desired result


I am testing the following regex:
(?<=\d{3}).+(?!',') This at regex101 regex

Test string:

187 SURNAME First Names                     7 Every Street, Welltown Racing Driver

The sequence I require is:

  1. Begin after 3 digit numeral
  2. Read all characters
  3. Don't read the comma

In other words:

 SURNAME First Names                     7 Every Street

But as demo shows the negative lookahead to the comma has no bearing on the result. I can't see anything wrong with my lookarounds.


Solution

  • .+ consumes everything.

    So (?!,) is guaranteed to be true.

    I'm not sure if using quotes is correct for whichever flavour of regex you are using. Bare comma seems more correct.

    Try:

    (?<=\d{3})[^,]+