views:

179

answers:

1

Is there any clean way to generate a regular expression that will match values formatted with a locale-configured java NumberFormat? Or some library that will do so?

This is in a web-based app & I want to pass the regex out to the jsp page for javascript validation on numeric fields. Locale varies on a per-user basis & I'd like to not have to hand-code specific regexes for every number format out there.

+1  A: 

Unfortunately DecimalFormat uses its own pattern language.

You can get the pattern string like this:

DecimalFormat nf = (DecimalFormat) NumberFormat.getCurrencyInstance();
// ...
String patter = nf.toLocalizedPattern();

Which on my system returns "¤#,##0.00"

You might be able to roll your own function that converts that to a regex (limited to the cases that are used in your application), but it's not in the standard library and I'm not aware of any third-party library that does it.

finnw
So you're saying this is my chance for glory...Thanks, this gets me part of way home. I'll hold out hope for a bit that someone else may chime in with the remainder.