java.util.regex 包主要包括三个类:
- Pattern类 : 用于创建一个正则表达式(模式匹配),它的构造方法是私有的,不可以直接创建;可以通过 Pattern.complie(String regex) 简单工厂方法创建一个正则表达式
- Matcher类 : Matcher 对象是对输入字符串进行解释和匹配操作的引擎
- PatternSyntaxException : PatternSyntaxException 是一个非强制异常类,它表示一个正则表达式模式中的语法错误
Pattern p = Pattern.compile(regex);
p.pattern();
compile(String regex) 将正则表达式编译到Pattern中
pattern() 返回正则表达式的字符串形式,其实就是返回Pattern.complile(String regex)的regex参数
用于分隔字符串
Pattern p=Pattern.compile("\\d+");
String[] str=p.split("我的姓名是:456456我的电话是:0532214我的邮箱是:[email protected]");
//str[0]="我的姓名是:" str[1]="我的电话是:" str[2]="我的邮箱是:[email protected]"
用于快速匹配字符串,该方法适合用于只匹配一次,且匹配全部字符串
Pattern.matches("\\d+","2223");//返回true
Pattern.matches("\\d+","2223aa");//返回false,需要匹配到所有字符串才能返回true,这里aa不能匹配到
返回一个Matcher对象
Pattern类只能做一些简单的匹配操作,要想得到更强更便捷的正则匹配操作,那就需要将Pattern与Matcher一起合作.
Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher("22bb23");
m.pattern();//返回p 也就是返回该Matcher对象是由哪个Pattern对象的创建的
Matcher类的构造方法也是私有的,不能随意创建,只能通过Pattern.matcher(CharSequence input)方法得到该类的实例
Matcher类提供了对正则表达式的分组支持,以及对正则表达式的多次匹配支持
matches()对整个字符串进行匹配,只有整个字符串都匹配了才返回true
Pattern.matcher(String regex,CharSequence input) <===> Pattern.compile(regex).matcher(input).matches()
Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher("22bb23");
m.matches();//返回false,因为bb不能被\d+匹配,导致整个字符串匹配未成功.
Matcher m2=p.matcher("2223");
m2.matches();//返回true,因为\d+匹配到了整个字符串
lookingAt()对前面的字符串进行匹配,只有匹配到的字符串在最前面才返回true
Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher("22bb23");
m.lookingAt();//返回true,因为\d+匹配到了前面的22
Matcher m2=p.matcher("aa2223");
m2.lookingAt();//返回false,因为\d+不能匹配前面的aa
find()对字符串进行匹配,匹配到的字符串可以在任何位置
Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher("22bb23");
m.find();//返回true
Matcher m2=p.matcher("aa2223");
m2.find();//返回true
Matcher m3=p.matcher("aa2223bb");
m3.find();//返回true
Matcher m4=p.matcher("aabb");
m4.find();//返回false
当使用matches(),lookingAt(),find()执行匹配操作返回 true 后,就可以利用以下三个方法得到更详细的信息
- start():返回匹配到的子字符串在字符串中的索引位置
- end():返回匹配到的子字符串的最后一个字符后一个在字符串中的索引位置
- group():返回匹配到的子字符串
Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher("aaa2223bb");
m.find();//匹配2223
m.start();//返回3
m.end();//返回7,返回的是2223后的索引号
m.group();//返回2223
捕获组是把多个字符当一个单独单元进行处理的方法,它通过对括号内的字符分组来创建。
捕获组是通过从左至右计算其开括号来编号。例如,在表达式((A)(B(C))),有四个这样的组:
- ((A)(B( C )))
- (A)
- (B( C ))
- ( C )
- 可以通过调用 matcher 对象的 groupCount 方法来查看表达式有多少个分组。groupCount 方法返回一个 int 值,表示matcher对象当前有多个捕获组。
- (group(0),它总是代表整个表达式。该组不包括在 groupCount 的返回值中
start(),end(),group()均有一个重载方法它们是start(int i),end(int i),group(int i)专用于分组操作
Pattern p=Pattern.compile("([a-z]+)(\\d+)");
Matcher m=p.matcher("aaa2223bb");
m.find(); //匹配aaa2223
m.groupCount(); //返回2,因为有2组
System.out.println("Found value: " + m.group(0) );
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
m.start(1); //返回0 返回第一组匹配到的子字符串在字符串中的索引号
m.start(2); //返回3
m.end(1); //返回3 返回第一组匹配到的子字符串的最后一个字符在字符串中的索引位置.
m.end(2); //返回7
m.group(1); //返回aaa,返回第一组匹配到的子字符串
m.group(2); //返回2223,返回第二组匹配到的子字符串
PatternSyntaxException 是一个非强制异常类,它指示一个正则表达式模式中的语法错误。
只有当匹配操作成功,才可以使用start(),end(),group()三个方法,否则会抛出java.lang.IllegalStateException,也就是当matches(),lookingAt(),find()其中任意一个方法返回true时,才可以使用。