3

To answer another user's question I knocked together the below regular expression to match numbers within a string.

\b[+-]?[0-9]+(\.[0-9]+)?\b

After providing my answer I noticed that I was getting unwanted matches in cases where there was a sequence of digits with more than one period among them due to \b matching the period character. For example "2.3.4" would return matches "2.3" and "4".

A negative lookahead and lookbehind could help me here, giving me a regex like this:

\b(?<!\.)[+-]?[0-9]+(\.[0-9]+)?\b(?!\.)

...except that for some unknown reason VBScript Regex (and by extension VBA) doesn't support lookbehind.

Is there some workaround that allows me to affirm that the word boundary at the start of the match is not a period without including it in the match?

Community
  • 1
  • 1
Aiken
  • 2,408
  • 2
  • 15
  • 25

1 Answers1

3

Perhaps you don't need a look behind. If you are able to extract specific capture groups instead of the entire match then you can use:

(?:[^.]|^)\b([+-]?([0-9]+(\.[0-9]+)))\b(?!\.)

Will match:

2.5      
54.5
+3.45
-0.5

Won't match:

1.2.3
3.6.
.3.5
  • Capture group 1 will output the whole number and sign
  • Capture group 2 will output the whole number
  • Capture group 3 will output the fraction (like capture group 1 in your original expression)
JonM
  • 1,315
  • 10
  • 14