DebugPointer
Published on

Regex for euro currency

Regex for euro currency

The euro is the official currency of the European Union (EU) and a number of other European countries. It is denoted by the symbol "€" and is divided into 100 smaller units called cents. In this article let's understand how we can create a regex for euro and how regex can be matched for euro.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Conditions to match euro currency

  • The value has to be a number with any number of digits
  • It can have a decimal point and a maximum of 2 decimal places
  • It can have a euro sign at the beginning
  • No other characters are allowed

Regex for checking if euro currency is valid or not

Regular Expression-

/^\€?[0-9]+(\.[0-9][0-9])?$/gm

Test string examples for the above regex-

Input StringMatch Output
€€1232.123does not match
€31.12matches
€32.121does not match
12.12matches

Here is a detailed explanation of the above regex-

/^\€?[0-9]+(\.[0-9][0-9])?$/gm

^ asserts position at start of a line
\€ matches the character € with index 836410 (20AC16 or 202548) literally (case insensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below [0-9]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
1st Capturing Group (\.[0-9][0-9])?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

Regex for checking if euro currency with comma is valid or not

Regular Expression-

/^\€?\d+(,\d{3})*(\.[0-9]{2})?€/gm

Test string examples for the above regex-

Input StringMatch Output
€€1232.123does not match
€1,231.12matches
€32.121does not match
12.12matches

Here is a detailed explanation of the above regex-

/^\€?\d+(,\d{3})*(\.[0-9]{2})?€/gm

^ asserts position at start of a line
\€ matches the character € with index 3610 (2416 or 448) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equivalent to [0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
1st Capturing Group (,\d{3})*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
\d matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
2nd Capturing Group (\.[0-9]{2})?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
Match a single character present in the list below [0-9]
{2} matches the previous token exactly 2 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
€ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and € to match the begin/end of each line (not only begin/end of string)

Hope this article was useful to check if euro currency is valid or not. If you have any doubts or suggestions, feel free to comment below.