正则 表达式手册
http://tool.oschina.net/uploads/apidocs/jquery/regexp.html
当需要找到匹配一个字符时
public class RegexTest {
public static void main(String
[] args
) {
String description
= "我的电话是:13888888888,有事联系";
String reg
= "(\\(\\d{3,4}\\)|\\d{3,4}-|\\s)?\\d{7,14}";
System
.out
.println(regx(description
, reg
));
}
public static String
regx(String description
, String reg
){
try {
Pattern p
=Pattern
.compile(reg
);
Matcher matcher
= p
.matcher(description
);
if (matcher
.find()) {
String group
= matcher
.group(0);
return group
;
}else
return null
;
}catch (Exception e
){
e
.printStackTrace();
}
return null
;
}
}
找出所有匹配项
public class RegexTest {
public static void main(String
[] args
) {
String str
= "5月1日,吃饭;5月21日,睡觉;12月22日敲代码";
String reg
= "\\d+月\\d+日";
System
.out
.println(regx(str
, reg
));
}
public static List
<String> regx(String description
, String reg
){
List
<String> list
= new ArrayList<>();
try {
Pattern p
=Pattern
.compile(reg
);
Matcher matcher
= p
.matcher(description
);
while (matcher
.find()) {
String group
= matcher
.group();
list
.add(group
);
}
}catch (Exception e
){
e
.printStackTrace();
}
return list
;
}
}
转载请注明原文地址: https://yun.8miu.com/read-22351.html