我的代码如下:
public static boolean RegExp(String regex, String matchstr) {
boolean ret = false;
try {
if ((regex==null)||(matchstr==null)) return ret;
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(matchstr);
ret = m.find();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public static void main(String[] args) {
String mstr = “<189>779852: Jul 30 11:41:26 CST: %LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet2/0/39, changed state to down”;
String rstr = “%LINEPROTO-\d-UPDOWN:.*GigabitEthernet([0-9a-zA-Z/]+),.*down”;
String ret = null;
boolean find = false;
find = RegExp(rstr,mstr);
System.out.println(“find = “+find);
}
程序运行没有问题,返回为 true。但是我放在在线测试工具:http://tool.oschina.net/regex上测试,却没有匹配上。请高手指正。先谢了。