joanfihu
joanfihu

Reputation: 336

Extract number from string using regular expressions [multiple formatting options]

I'm scratching my head trying to come up with a regex that extracts numbers from strings that are differently formatted. For example:

'1', '1.1', '1,1', '1,000,000.20', '1.00000020', '1.000.000,20', '10.20001'

I currently use the regex [-+]?[0-9]*[.,]?[0-9]+(?:[eE][-+]?[0-9]+)? and it works well in the majority of the cases except from 1,000,000.20 and 1.000.000,20.

Do you have any idea how can I tweak the previous regex to work with those examples?

Upvotes: 1

Views: 331

Answers (1)

KyleFairns
KyleFairns

Reputation: 3057

(?!\d+,\d+\.\d+,|\d+\.\d+,\d+.)^([+-]?(?:\d+|\d{1,3}(?:[.,]\d{3})*)(?:[.,]\d+|[eE][+-]?(?:\d+|\d{1,3}(?:[.,]\d{3})*))?)$

Perhaps something like this?

This will match all of the ones you stated, plus numbers written in the format 1e10 and 1e-9.

It will also not match numbers where there are inconsistencies in the comma dot format, i.e 10.234245,214,10.234,245.214 or 10,234.245,214

Also will allow for + or - at the beginning of these numbers

Check it out on Regex101

Upvotes: 1

Related Questions