0228

Pattern:创建一个正则表达式匹配模式
Matcher:匹配输入是否匹配该regex
{n,m}表示它前面的东西可以出现n~m次。
{,m}最多出现m次。
{n,}最少n次。
{n}必须n次。
其余P264
贪婪模式(Greedy)(无)
勉强模式(Reluctant)(?)
占有模式(Possessive)(+)
有效的上海固话System.out.println("021-12345678".matches("021-\\d{8}"));   返回Boolean
String id="123456190000000000";          id.matches("\\d{6}1[89]\\d{2}[01]\\d[0-3][0-9]\\d{3}[0-9X]")   不完善

regex可用于爬电话号邮箱之类的:
import java.util.regex.*;
public class FindGroup
{
public static void main(String[] args)
{
String str="发票联系13012345678"+"办证[email protected]";
Pattern p=Pattern.compile("(13\\d{9})|(\\w{8,20}@\\w*.(com|net))");
Matcher m=p.matcher(str);
boolean b=m.matches();
//相当于
//Matcher m=Pattern.compile("(13\\d{9})|(\\w*@\\w*.com)");
                                  //方法P266
while(m.find())
{
System.out.println(m.group());
}
}

}

猜你喜欢

转载自huadianfanxing.iteye.com/blog/2359753