简体   繁体   中英

java generate regular expression from localized NumberFormat

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM