一段测试程序,供参考BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); Pattern p = Pattern.compile("[\\d\\.]*"); String s; while((s = r.readLine())!= null){ Matcher m = p.matcher(s); System.out.println(m.matches()?"match":"not match"); System.out.println(" "); }输出: hi not match
可以匹配 "", "1", ".", "..", ".12", "1.2" 等形式,不知道是不是你想要的。
Pattern p = Pattern.compile("[\\d\\.]*");
String s;
while((s = r.readLine())!= null){
Matcher m = p.matcher(s);
System.out.println(m.matches()?"match":"not match");
System.out.println(" ");
}输出:
hi
not match
3.4
match
5.6
match
d
not match
2.77b
not match