Search code examples
regexpcreregex-lookaroundsnegative-lookbehind

Using a word boundary, how to match a string of numbers unless there is a period before the numbers?


I am trying to match a set of numbers that refer to items. The items may be separated by linebreak, a comma, a colon, a dash or a space. The items would not be separated by a period or an @ sign.

We were using \b to denote the start of the items, but have found this to capture all sorts of undesired matches, such as reallythisurl@6001999 and reallythis.6001999

We tried the negative lookahead: \b(?!\.)(601999) \b(?!\@)(601999)

But this did not stop from matching numbers that came after a period or an @


Solution

  • Just add negative lookbehind: (?<![@.])\b\d+

    (?<![@.]) - assures that what preceeds is not @ or .

    \b - word boudnary

    \d+ - one or more digits

    Demo