当前位置: 代码迷 >> J2SE >> IP 正则表达式有关问题
  详细解决方案

IP 正则表达式有关问题

热度:395   发布时间:2016-04-24 13:15:34.0
IP 正则表达式问题
String pattern = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}";
  String value = "192.168.4.35"

  Pattern p = Pattern.compile(pattern);
  Matcher m = p.matcher(value);
   
这样能match,


  String pattern = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}";
  String value = "asdad 192.168.4.35 asdada"

  Pattern p = Pattern.compile(pattern);
  Matcher m = p.matcher(value);

不match?

------解决方案--------------------
var sIPAddress = form.appIp.value;
var exp=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
var reg = sIPAddress.match(exp);
var ErrMsg="你输入的是一个非法的IP地址段!\nIP段为::xxx.xxx.xxx.xxx(xxx为0-255)!";
------解决方案--------------------
Java code
String pattern = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"; String value = "asdad 192.168.4.35 asdada";Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(value);if(m.find()) {   System.out.println("success.");}
------解决方案--------------------
Java code
(".*?\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}.*?"))
------解决方案--------------------
楼上说的是不是用这个。
Java code
Pattern = Pattern.compile(".*?((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d).*?");