Help With Regular expressions
Hi All,
Can someone help me out in writing a regular expresstion for the following 2 patterns
The used pattern is:
###.###,##
The regular expression needs to match the thousands as they grow
For eg,
Valid Input can be :
100.454.777,22
10.454.777,22
Invalid input
1004.454.777,22
1.23.444,22
Thanks in advance
Manikantan
You should use the java.text.DecimalFormat
If the pattern you need doesn't correspond to the default one (for your Locale), you need to build your own DecimalFormatSymbols.
For example:java.text.DecimalFormatSymbols symbols = new java.text.DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
symbols.setGroupingSeparator('.');
System.out.println(symbols.getGroupingSeparator());
java.text.DecimalFormat dFormat = new java.text.DecimalFormat("#,###.##", symbols);
System.out.println(dFormat.format(1234567.12));
> You should use the java.text.DecimalFormat
I agree with this if you're simply trying to pretty-print a number. However, if you're doing validation on user entered data, I've come up with this example of a regular expression that seems to be working.
public static void main(String[] args) {
String regularExpression = "^([0-9]{1,3})(\\.[0-9]{3,3})*(,[0-9]{2,2})$";
Pattern pattern = Pattern.compile(regularExpression);
String[] testStrings = new String[7];
testStrings[0] = "100.454.777,22"; // match
testStrings[1] = "10.454.777,22"; // match
testStrings[2] = "1004.454.777,22"; // no match
testStrings[3] = "1.23.444,22"; // no match
testStrings[4] = "1.2342.444,22"; // no match
testStrings[5] = "123.23.444,22"; // no match
testStrings[6] = "12.354.869.456.223.444,22"; // match
for (int i = 0; i < testStrings.length; i++) {
Matcher matcher = pattern.matcher(testStrings[i]);
if (matcher.find()) {
System.out.println("Match at index: " + i);
} else {
System.out.println("No match at index: " + i);
}
matcher.reset();
}
}
Josh, you're right.
I never noticed that DecimalFormat is able to parse almost anything, without strictly applying pattern.
For example input "12.34,56" will be parsed without problem (e.g. no ParseException thrown) and give 1234.56 number.
And "12,34.56" will return 12.34.
No good for input validation...
I wouldn't be surprised if the jakarta commons validatior doesn't already have a class for this.
Hi All,
Thanks for the timely solution.
I thought i could ask more help frm you.
I need a regular expression for the following condition:
I need to validate that the user has not entered the "%" or the _ except that it is optionally allowed as the first character of the sentence/string.
Example : Valid Input :- %This is a string
_This is a string
This is a string
Invalid Input :- This is % a string
This is a _ string _%
Thanks in advance
Regards
Manikantan
Hi All,
Thanks for the timely solution.
I thought i could ask more help frm you.
I need a regular expression for the following condition:
I need to validate that the user has not entered the "%" or the _ except that it is optionally allowed as the first character of the sentence/string.
Example : Valid Input :- %This is a string
_This is a string
This is a string
Invalid Input :- This is % a string
This is a _ string _%
Thanks in advance
Regards
Manikantan
> I need to validate that the user has not entered the
> "%" or the _ except that it is optionally allowed as
> the first character of the sentence/string.
>
> Example : Valid Input :- %This is a string
> _This is a string
> This is a string
>
> Invalid Input :- This is % a string
> This is a _ string _%
>
> Thanks in advance
>
> Regards
> Manikantan
Are you doing homework on regular expressions?
/Kaj
> I need a regular expression for the following
> condition:
>
> I need to validate that the user has not entered the
> "%" or the _ except that it is optionally allowed as
> the first character of the sentence/string.
>
^[_%]?[^_%]+$