0

I'm trying to match any combination of float numbers with a regular expression in VBA inside of Microsoft Word.

Example:

<text="">
<text="1">
<text="2">
<text="3">
<text="3.5">
<text="4">
<text="44">

I've tried the following patterns:

regex.Pattern = "<text=" & Chr(34) & "\d*\.*\d*" & Chr(34) & ">"
regex.Pattern = "<text=" & Chr(34) & "[\d\.]*" & Chr(34) & ">"

What's interesting is neither of these patterns will match the 44 either.

Delimitry
  • 2,987
  • 4
  • 30
  • 39
Joel
  • 43
  • 1
  • 7

2 Answers2

0

from the regex tester site,

Positive Integers --- ^\d+$
Negative Integers --- ^-\d+$
Integer --- ^-{0,1}\d+$
Positive Number --- ^\d*\.{0,1}\d+$
Negative Number --- ^-\d*\.{0,1}\d+$
Positive Number or Negative Number - ^-{0,1}\d*\.{0,1}\d+$

to test for the empty result being valid, and assuming all positive numbers, you would end up with

regex.Pattern = "<text=" & Chr(34) & "\d*\.{0,1}\d*" & Chr(34) & ">"
SeanC
  • 15,695
  • 5
  • 45
  • 66
  • You'll notice this isn't different from my pattern other than you say 0 or 1 decimals, and I allow any. Also, doesn't work for me still. – Joel Nov 14 '12 at 16:21
  • a float will only have 0 or 1 decimals. 1.1.1 is not a valid number – SeanC Nov 14 '12 at 16:22
  • Additionally, you're very off base with your use of ^ and $, as my sample text shows it's clearly not the beginning or end of line. – Joel Nov 14 '12 at 16:23
  • fixed example. the $ and ^ were from me testing the number part only – SeanC Nov 14 '12 at 16:23
0

Let's break it down. A floating point number has two parts, integral and decimal. There are some choices on the format:

Here is pattern for a pretty common notation:

regex.Pattern = "<text=" & Chr(34) & "(-?\d+(\.\d+)?)?" & Chr(34) & ">"

Some choices:

  • Do you want to support omitting the integral part? (e.g. '.001')
    Use * instead of + in the first number match: (-?\d*(\.\d+)?)?

  • Do you want to support leading zeros? (e.g. 000.)
    This is gonna be more tricky :)

  • Do you want to support decimal point without decimal part? (e.g. '12.')
    Then add ? after the decimal point match: (-?\d*(\.?\d+)?)?

  • Is the decimal part mandatory?
    Remove the very last ?: (-?\d*(\.?\d+))?

qstebom
  • 719
  • 4
  • 12