简体   繁体   中英

Regex to extract multiple numbers with decimal

I have the following group of numbers.

SalesCost% Margin

2,836,433.182,201,355.6422.39

I want to separate this and extract the numbers such that I get the result as shown below:

2,836,433.18

2,201,355.64

22.39

I tried the Regex (\d+)(?:\.(\d{1,2}))? but this only extracts the number until the first decimal, ie I only get 2,836,433.18. Is there a way I can extract the numbers using Regex (or alternatively someway through Python) to get the results shown above?

Thanks

You can use

re.findall(r'\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?', text)
re.findall(r'(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{1,2})?', text)

See the regex demo #1 and regex demo #2 .

Details :

  • \d{1,3} - one, two or three digits
  • (?:,\d{3})* - zero or more occurrences of a comma and three digits
  • (?:\.\d{1,2})? - an optional sequence of . and one or two digits.

The (?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{1,2})? variation supports numbers like 123456.12 , ie no digit grouping symbol containing integer parts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM