0

The regexp for a US number as suggested here:

^[+-]?(\d*|\d{1,3}(,\d{3})*)(\.\d+)?\b$

works perfectly fine. I tried several examples using this service https://regex101.com/

However, using tcl's regexp with as follows:

regexp { ^[+-]?(\d*|\d{1,3}(,\d{3})*)(\.\d+)?\b$ } 120.00

returns '0'.

Have I arrived in tcl "Quoting Hell"?

Community
  • 1
  • 1
user3780948
  • 86
  • 1
  • 4
  • If your objective is to study regular expressions, numeric strings are a good field to work in. If your objective is to recognize numeric strings, the invocation `string is double -strict [string map {, {}} $candidate]` is a lot faster and less messy. – Peter Lewerin Mar 17 '17 at 17:14

1 Answers1

0

\b is a backspace in tcl regular expressions. I suspect you want \m and \M

% regexp -inline {\m[+-]?(\d*|\d{1,3}(,\d{3})*)(\.\d+)?\M} " 120.00 "
120.00 120 {} .00

I don't know if you meant to match spaces on either side of the number as well. But everything between the curly braces is part of the regular expression so you are looking for a space followed by optionally plus or minus followed by digits etc. Terminated by another space. As you wrote it, there are no spaces in your test string.

patthoyts
  • 32,320
  • 3
  • 62
  • 93