测试匹配整个字符串,然后找出符合字符串当中匹配子正则表达式的子字符串
?
例如:匹配只有3到5个数字,只有两个
?
public class Fenzu {
public static void main(String[] args) {
Pattern p = Pattern.compile("(\\d{3,5})([a-z]{2})");
String s = "123aa-3434bb-234ccc-00";
Matcher m = p.matcher(s);
//分组查找是找出满足全部正则表达式的条件中之后再进行“局部”筛选
while(m.find()){
System.out.println(m.start());
//打印出满足p模式中的分组1——只有数字
System.out.println("符合3到5个数字 第一个分组:" + m.group(1));
System.out.println("符合有两个字母的 第二个分组: " + m.group(2));
System.out.println(m.end());
}
}
}
?
?
边界小Demo
?
public class boundaryTest {
public static void main(String[] args) {
//以h开头的字符
out("help sir".matches("^h"));
//以h开头的字符串
out("hello sir".matches("^h.*"));
//表示以ir结尾的字符串
out("hello sir".matches(".*ir$"));
//\b表示的是单词之间的空格
out("hello sir".matches("^h[a-z]{1,3}o\\b.*"));
out("hellosir".matches("^h[a-z]{1,3}o\\b.*"));
}
public static void out(Object o){
System.out.println(o);
}
}
?