RegEx Pattern that matches positive or negative values (e.g "1.2", "-2.8", "7.8", -22.8")
decimal sepera开发者_运维问答tor is a dot, followed by max one digit! No range specified.
Thanks guys!
^-?\d+(\.\d)?$
if the decimal part is optional, and
^-?\d+\.\d$
if it's required :)
Simple: -?\d+\.\d
Unlikely to be relevant in this case, but don't forget that "." is not universal as the decimal separator. Many European countries use "," so you might prefer to get the one in use from the locale:
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
String separator = df.getDecimalFormatSymbols().getDecimalSeparator();
(See also: http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormatSymbols.html#getDecimalSeparator)
精彩评论