现在的位置: 首页 > 综合 > 正文

java 正则表达式

2018年01月28日 ⁄ 综合 ⁄ 共 321字 ⁄ 字号 评论关闭

java 正则表达式用法:

public static void main(String[] args) {
String a = "110 (样张)121";
Pattern pattern = Pattern.compile("(\\d+)");
Matcher matcher = pattern.matcher(a);
if(matcher.find()){
System.out.println(matcher.group());
}
}

在group之前,必须用find()方法,否则会报错,Exception in thread "main" java.lang.IllegalStateException: No match found异常。

而find()还可以用find(int i)来换。

抱歉!评论已关闭.