Java generates regular expression from localized NumberFormat

Is there any clean way to generate a regex that matches values ​​formatted with java NumberFormat tuned to locale? Or some library that does this?

This is in a web application and I want to pass the regular expression to the jsp page to validate javascript in numeric fields. The locale varies for each user, and I would not want to manually specify specific regular expressions for each number format.

+4
source share
1 answer

Unfortunately, DecimalFormat uses its own template language .

You can get the template string as follows:

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

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

You may be able to flip your own function, which converts it to a regular expression (limited to the cases that are used in your application), but this is not in the standard library, and I do not know any third-party library does this.

+1
source

Source: https://habr.com/ru/post/1300444/


All Articles