你直接用matchs判断就可以了,matcher是只要内部有能匹配上的就算满足,不是判断整体。
String rexp = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}"String addr = "11111.1.1.1"
System.out.println("1.1.1.1".matches(rexp))
System.out.println("11.1.1.1".matches(rexp))
System.out.println("111.1.1.1".matches(rexp))
System.out.println("11111.1.1.1".matches(rexp))
true
true
true
false
以下正则均验证11位数字,数字中不能有空格等其它字符,均按照java语法使用两个反斜杠转义验证AA
^(?=.*(.)\\1)1[358][0-9]{9}$
验证AABB
^(?=.*(.)\\1(.)\\2)1[358][0-9]{9}$
验证ABC
^(?=.*(.)(?!\\1)(.)(?!\\2))1[358][0-9]{9}$
验证符合ABC,也符合 AA
^(?=.*(.)(?!\\1)(.)(?!\\2))(?=.*(.)\\3)1[358][0-9]{9}$
验证尾数为6
^1[358][0-9]{8}6$
验证尾数为8
^1[358][0-9]{8}8$
验证尾数不为4
^1[358][0-9]{8}[0-35-9]$