**
代码如下:
**
import java.util.regex. * ;
public class E {
public static void main( String args[ ]) {
String s = "10月1日至11月6日禁止通行";
String regex =" [ 0 - 9 ] + [月曰] "; //正则表达式
Pattern p = Pattern.compile(regex); //Pattern.compile()方法获取正则表达式
Matcher m = p.matcher(s); //调用matcher()传入一个字符串
int count = 0;
String item =" ";
while(m.find()) {
item = m.group (); //循环,获取与正则表达式匹配的 xx月 xx日 给item
count ++; //计数
}
/*
Boolean b=m.find() 从这个字符串中开始匹配 ,匹配到xx月 xx日 返回true
String s11=m.group() 从这个字符串中获取到手机号
*/
//最后匹配得m.group()=6日 一共循环四次
System.out.printf(" %d: %s",count,item); //最后输出 4:6日
}
}
**
图片更直观:
**