java generate regular expression from localized NumberFormat
Is there any clean way to genera开发者_运维百科te 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.
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.
精彩评论