The dollar is the official currency of the United States of America and a number of other countries. It is denoted by the symbol “$” and is divided into 100 smaller units called cents. The dollar is the world’s most widely used currency and is recognized as the reserve currency of the world. In this article let’s understand how we can create a regex for dollar and how regex can be matched for dollar.
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 dollar amount
- 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 dollar sign at the beginning
- No other characters are allowed
Regex for checking if dollar amount is valid or not
Regular Expression-
/^\$?[0-9]+(\.[0-9][0-9])?$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
$$1232.123 | does not match |
$31.12 | matches |
$32.121 | does not match |
12.12 | matches |
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 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)
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 sensitive)
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 sensitive)
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 sensitive)
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 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)
Regex for checking if dollar amount with comma is valid or not
Regular Expression-
/^\$?\d+(,\d{3})*(\.[0-9]{2})?$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
$$1232.123 | does not match |
$1,231.12 | matches |
$32.121 | does not match |
12.12 | matches |
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 dollar amount is valid or not. If you have any doubts or suggestions, feel free to comment below. In conclusion, understanding and utilizing regular expressions (regex) for validating dollar amounts is essential for effective text manipulation and pattern recognition. With the provided regex patterns, you can accurately match valid dollar amounts, both with and without commas, ensuring precise validation for various scenarios. By harnessing the power of regex, programmers and developers can enhance their text-processing capabilities and build more robust applications.