0

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

Zoey Malkov
  • 776
  • 6
  • 16
  • See https://stackoverflow.com/questions/5917082/regular-expression-to-match-numbers-with-or-without-commas-and-decimals-in-text – The fourth bird Oct 28 '21 at 22:10
  • If you are matching just a part of a string don't bookend your regex with `^` and `$`, as `^` means assert start of string and `$` means assert end of string. For example, `^\d` means match a digit at the start of the string only, and `\d$` means match a digit at the end of the string only. – MikeM Oct 28 '21 at 22:25

2 Answers2

2

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

[0-9,.]+

https://regex101.com/r/QGtdsO/2/

enter image description here

Chris Doyle
  • 10,703
  • 2
  • 23
  • 42
0

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
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37