Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regular Expression to match dollar amounts

Tags:

java

regex

This is what i've been using

\$?[0-9]+\.*[0-9]*

But when i was doing some testing i noticed that things like

$$$34.00 

would return as a match (but matcher.group()) just returns the matched substring. I don't want it to even pass the regular expression if the user enters more than one dollar sign so i tried this:

\${1}[0-9]+\.*[0-9]*

but this seems to behave the same as the regular expression i first typed. Right now i'm testing this in java but, i plan to use it in c++ using the Boost libraries. But Please don't give me that solution here because i'm trying to learn without someone giving me the answer.

But i do need help making it so the user can only enter one dollar sign (which is what i thought \${1} would do)

like image 934
rage Avatar asked Jul 25 '13 16:07

rage


People also ask

How do I match a number in regex?

To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.

How do you match numbers in Java?

Matching Digits You can match digits of a number with the predefined character class with the code \d . The digit character class corresponds to the character class [0-9] . Since the \ character is also an escape character in Java, you need two backslashes in the Java string to get a \d in the regular expression.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).


4 Answers

I would suggest avoiding the use of regular expressions for currency parsing, esp when Java provides you much simpler ways to solve this problem.

Consider this code:

String str = "$789.11"; // user entered value
Number number = null;
try {
    number = NumberFormat.getCurrencyInstance(Locale.US).parse(str);
} catch(ParseException pe) {
    // ignore
}

if (number != null) {
   // proceed as user entered a good value
}
else {
   // user didn't enter a good value
}
like image 91
anubhava Avatar answered Nov 15 '22 07:11

anubhava


Since you're doing this to learn regex...

^\$(([1-9]\d{0,2}(,\d{3})*)|(([1-9]\d*)?\d))(\.\d\d)?$

Breakdown:

^\$ start of string with $ a single dollar sign

([1-9]\d{0,2}(,\d{3})*) 1-3 digits where the first digit is not a 0, followed by 0 or more occurrences of a comma with 3 digits

or

(([1-9]\d*)?\d) 1 or more digits where the first digit can be 0 only if it's the only digit

(\.\d\d)?$ with a period and 2 digits optionally at the end of the string

Matches:

$4,098.09
$4098.09
$0.35
$0
$380

Does not match:

$098.09
$0.9
$10,98.09
$10,980456
like image 24
Syon Avatar answered Nov 15 '22 06:11

Syon


You can do something like [^\$]*\$? in the beginning. This would insure that there are no duplicate $ signs, but also matches if there is no $ present.

Also, if you are working with currency (possible decimal and 2 digits after), you should use [\.\d{2}]?.

This says that it can be a match if it's followed by ONE instance of a period and 2 digits or nothing at all. As stated in the comments, it can also match multiple periods in a row, so you shouldn't use the * quantifier after \.

like image 45
Song Gao Avatar answered Nov 15 '22 06:11

Song Gao


You are missing ^(beginning of string),$(end of string)

^\$\d+([.][0-9]+)?$
like image 27
Anirudha Avatar answered Nov 15 '22 08:11

Anirudha