Home > Software engineering >  Regex match numbers and decimals with and without comma
Regex match numbers and decimals with and without comma

Time:10-29

I want to match numbers/decimals even if there are commas and decimals. For example, I want it to be able to match in a sentence. The number could be in the middle of a sentence, at the start or end:

the number is 1000 today
the number is 1000.00 today
the number is 1000.142 today
the number is 1000.30 today
the number is 1000.200 today
the number is 1,000 today
the number is 1,000.00 today
the number is 1,000.142 today
the number is 1,000.30 today
the number is 1,000.200 today

I have tried ^\d*\.?\d $ but doesn't seem to be working

CodePudding user response:

What about just keeping it simple if this is really your source data.

[0-9,.] 

enter image description here

CodePudding user response:

Use

(?:\d{4,}|\d{1,3}(?:,\d{3})*)(?:\.\d )?

See regex proof.

EXPLANTION

--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    \d{4,}                   digits (0-9) (at least 4 times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    \d{1,3}                  digits (0-9) (between 1 and 3 times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      ,                        ','
--------------------------------------------------------------------------------
      \d{3}                    digits (0-9) (3 times)
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    \d                       digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
  • Related