正常正数负数都没问题
非法数字就不行
例如
1.1.1
1.a
.124
a.1
非法数字就不行
例如
1.1.1
1.a
.124
a.1
解决方案 »
- java问题,求大牛们解答
- 写二进制文件的问题
- JTableheader与tableColumn,有什么区别?
- 求好的英文技术网站 最好是论坛,博客都有的,
- 急求解决办法:文本很大,怎样一边读一边写 用filereader 和filewriter会抛出java.outofememory.exception 有没有别的办法 谢谢
- 请教问题 关于mysql和java
- Swing 高手请进
- java or oracle 技术群
- 不知为何报错,请高手指教!
- weblogic6无法启动.
- 求java趣味案例或趣味题,在线等
- 这里的多线程中的sleep方法为什么不能用?我加了try和catch,还是不行?
boolean flag=str.matches("(-?)(\\d+|([0]|[1-9]+)\\.\\d+)");
System.out.print(flag);
String str="-1.001";
boolean flag=str.matches("(-?)(\\d+|([0]|[1-9]\\d+)\\.\\d+)");
System.out.print(flag);
应该这样
System.out.print(str.matches("[-+]?([1-9]\\p{Digit}*|[0])(.\\p{Digit}*[1-9])?"));可以验证通过带正负号的数字,小数部分可有可无。数字前头后头(小数最后)不能有0,验证通过的数字举例:0 10 1.5 0.1
验证不通过的数字举例:0.0 -00.1 +1.00 12. .12 +.12 -0.
import java.util.regex.*;public class Test {
static String input="";
static Pattern p=Pattern.compile("^(-?)([0]|([1-9]\\d+))([.]\\d+)?$");
static void test(){
Matcher m=p.matcher(input);
System.out.println(m.find());
}
public static void main(String[] args) {
input="-11.22";
test();
input="11.";
test();
input="11.22.33";
test();
input="01.2";
test();
input=".123";
test();
}}
String str="-1";
boolean flag=str.matches("(-?)([0]|[1-9]\\d*)(\\.\\d+)?");
System.out.print(flag);
boolean flag=str.matches("(-?)([0]|[1-9]\\d*)(\\.\\d+?)");
System.out.print(flag);
修正版
static void test(String input){
System.out.println(input.matches("(-?)([0]|([1-9]\\d+))([.]\\d+)?"));
}
public static void main(String[] args) {
test("123");
test("-123");
test("11.22");
test("-11.22");
test("0.123");
test("-0.123");
test("-11.");
test("11.22.33");
test("-01.2");
test(".123");
test("a.2");
}
}
似乎没问题啊