2

I am trying to create a regular expression that matches money amount (various currencies either in front of or after the given amount. The decimals are separated by a dot or by a comma).

This is what I've got so far:

\$[0-9.,]+|\£[0-9.,]+|\€[0-9.,]+

However, if I put currencies in the square brackets together with the other signs, it does not work as I expect it to (it still doesn't match 20,000$, only $20,000 and I want it to match both).

Can you tell me how I can modify my regex so that it also matches the amounts with the currency after the digits?

Also, is the only way to include more than one currency in the regex to separate them with a pipe and rewrite the same regular expression over and over again?

Luvama21
  • 21
  • 1

1 Answers1

2

Updated:

This regex should match numbers with decimal group separators (zero or more) and a decimal point (zero or one):

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

For your use-case you should be happy with this regex:

[\$£€](?:\d{1,3},)*\d{1,3}(?:\.\d/{1,2})?|(?:\d{1,3},)*\d{1,3}(?:\.\d{1,2})?[\$£€]

Legacy answer

There is no way in regular expressions (at least that I know of) that would allow you to swap the order of two groups of characters, thus you'll have to specify it like "AB or BA".

Hope, this one works for you:

[\$\£\€]\d+(?:[.,]\d+)?|\d+(?:[.,]\d+)?[\$\£\€]

The \d+(?:[.,]\d+)? part could be simplified back to [\d.,]+. The simplest for of regex (with a lot of information lost) is this:

[\$£€]?[\d.,]+[\$£€]?

... but that allows a lot of erroneous inputs, like 20.$ or $.,€ or simply 5.

Parzh from Ukraine
  • 7,999
  • 3
  • 34
  • 65
  • Thank you very much! This is very close to what I need. I tested it on https://regexr.com/ and unfortunately, it doesn't match an amount which includes more than one decimal separator (i.e. a comma/dot). When the name of the currency is in front of the amount, it matches the first two decimal groups (e.g. in case of $20,000,000 it only matches $20,000). When the name of the currency stands after the amount, it matches the last two 3-digit decimal groups (e.g. in the case of 20,000,000$ it only matches 000,000$). – Luvama21 Aug 01 '18 at 20:37
  • Oh, I confused the decimal group separator with a decimal point. – Parzh from Ukraine Aug 01 '18 at 20:45
  • Yes, thank you, now it works for commas, but there's still the same problem with dots. I tried to change it a little bit myself so that it matches the amounts with dots as the decimal group separators, but it didn't work. Could you explain to me why the "dot part" can't look like that (?:\d{1,3}.) (as it is with a comma)? – Luvama21 Aug 01 '18 at 21:41
  • Because it is a special character meaning "any character" – Parzh from Ukraine Aug 01 '18 at 22:02
  • Yes, I understand why it has to be preceded by a slash, my mistake. I understand the division between A and B parts and the parts with a comma (basically up to *). Then, I don't understand this part of regex regarding the dot: \d{1,3}(?:\.\d+)? which I would like to change as it again only matches 2 decimal groups. If I understand correctly, it first matches 1-3 digits and then the dot and after that it again matches digits. So why can't it look like that: (?:\d{1,3}\.)? (obviously, I have checked it and it doesn't work). – Luvama21 Aug 01 '18 at 22:13
  • I've edited the regex to match only two digits after the decimal point. It cannot be like you suggested because there has to be at least one digit after the decimal point, and `(?:\d{1,3}\.)` allows zero digits after the period. – Parzh from Ukraine Aug 01 '18 at 22:23
  • I see. Thank you! I've modified it a little bit because it still only properly matched the amounts with commas as decimal separators. But your improvement helped me a lot to change it, thank you so much for all your help! It looks like that now: `[\$£€](?:\d{1,3},)*\d{1,3}(?:\.\d{1,3})*|(?:\d{1,3},)*\d{1,3}(?:\.\d{1,3})*[\$£€]` I've tested it and (so far ;) ) it works as I intended it to. – Luvama21 Aug 02 '18 at 11:55