正则表达式和数字验证

前端之家收集整理的这篇文章主要介绍了正则表达式和数字验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

import java.util.regex.*;


public class ValidationNumber {

public static boolean isintNumber(String aString) {
// 检查一个字符串是否是整数 允许一个正负符号位

if (aString == null || aString.length() < 1) {
return false;
}

Pattern intnumPattern = Pattern.compile("^[+-]?[\\d]+$");
return intnumPattern.matcher(aString).find();
}

public static boolean isNumber(String aString) {
// 检查一个字符串是否全部是数字

if (aString == null || aString.length() < 1) {
return false;
}

Pattern numberPattern = Pattern.compile("^[\\d]+$"); // "^[+-]?([\\d]+)|([\\d]+\\.[\\d]+)$"
return numberPattern.matcher(aString).find();
}

public static boolean isdoubleNumber(String aString) {
//检查一个字符串是否是浮点小数,浮点小数至少一个数字开头,允许一个正负符号位
//中间只出现一个小数点,小数点之后至少一个数字

if (aString == null || aString.length() < 1) {
return false;
}

Pattern doublenumPattern = Pattern.compile("^[+-]?[\\d]+\\.[\\d]+$");
return doublenumPattern.matcher(aString).find();
}

public static void main(String[] args) {
// 测试
String intnum1 = "-23423";
String intnum2 = "234";
String numstring = "234sdf";
String num1 = "234234";
String doublenum1 = "0.123456";
String doublenum2 = "-0.23";
String doublenum3 = ".123123";

System.out.println("" + isintNumber(intnum1));
System.out.println("" + isNumber(num1));
System.out.println("" + isNumber(numstring));

System.out.println("" + isdoubleNumber(doublenum1)); }

原文链接:https://www.f2er.com/regex/359166.html

猜你在找的正则表达式相关文章