3

I need to get these number formats from a string

20000
20 000
20 000 000
20 000,00
20 000.00
2000,000
200000.000

Strings can be

some text 20000
20000 some text 

So far I have this

/((\d){1,}[ .,]{1}[0-9]{1,}[ .,]{1}[0-9]{1,})/g

Thank you.

3
  • Explain the number 2000,000 and what this means. Commented Apr 2, 2020 at 5:55
  • In 2000,000 (not 2,000,000) does the comma translate as a period in English? Commented Apr 2, 2020 at 5:55
  • Yes, it is a period in English. Commented Apr 2, 2020 at 6:19

1 Answer 1

3

I would use this regex:

^[1-9][0-9]{0,2}(?:[ ,]?\d{3})*(?:[,.]\d+)?$

Demo

Here is an explanation of the regex:

^                from the start of the string
[1-9][0-9]{0,2}  match 1 to 3 leading digits (first digit not zero)
(?:[ ,]?\d{3})*  then match an optional thousands separator followed by 3 digits,
                 the entire quantity zero or more times
(?:[,.]\d+)?     match an optional decimal component, using either , or . as the separator
$                end of the string
Sign up to request clarification or add additional context in comments.

3 Comments

@ Tim Biegeleisen How can I use it on string like this 20 000 some text. Looks like it matches only numbers. Thank you.
Replace the ^ and $ anchors with word boundaries, i.e. use \b[1-9][0-9]{0,2}(?:[ ,]?\d{3})*(?:[,.]\d+)?\b
@ Tim Biegeleisen Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.