Home > OS >  How can I modify this regular expression to find a wide range of number variations with commas and d
How can I modify this regular expression to find a wide range of number variations with commas and d

Time:05-05

This expression…

\$?\.?\d ,?\.?\d \%?

…finds these numbers exactly as intended…

$2130.42
$1,500
$14
20.0
3000
72,000
1.5
.50
0.5
50%
50.2%

And it ignores these numbers also as intended…

(5)
#2

BUT if I add one or more commas, with or without a dollar sign, the number is found as separate matches as in…

  • $2,130 .42
  • 2,333 , 130.42
  • 2,444 , 333,130 .42

Is there a simple modification I can make to the existing expression to solve this problem (first preference); and if not, what expression would work?

I don't care about the proper number of digits between the commas, nor multiple decimal points, because I want to allow for entry errors (e.g., 14,22.2) and will be processing the number anyhow.

CodePudding user response:

Add a term to match any number of comma-digits sequences, and a look behind at the start for either start of input or a space to prevent matching (5) etc:

(?<=^| )\$?\.?\d (,\d )*(\.\d )?%?

See live demo.

Same regex but with non capturing groups (longer, but a little faster/simpler for the engine):

(?<=^| )\$?\.?\d (?:,\d )*(?:\.\d )?%?
  • Related