I want to have regex that will match numbers that are not preceded by whitespace or punctuation, e.g.:

  • foo12 -> matches 12
  • foo 42 -> no match
  • foo.42 -> no match

I came up with: (?<![[:space:][:punct:]])\d+

However, this does not work as I intend, as for the examples, the results are as folllows:

  • foo12 -> matches 12 (OK)
  • foo 42 -> matches 2 (not OK)
  • foo.42 -> matches 2 (not OK)

I understand, why it matches single digits in the last two examples (because negative-lookbehind includes only whitespace and punctuation), however I am not sure how to change my regex to exclude those matches. How can it be corrected?

2 Answers

revo On Best Solutions

The reason for partial match is that engine doesn't know exactly where it should start from regarding your requirements. You tell engine by including \d in character class:

Emma On

This RegEx might help you to divide your string input into two groups, where the second group ($2) is the target number and group one ($1) is the non-digit behind it:



It might be safe to do so, if you might want to use it for text-processing.