[01] Pattern类和Matcher类
- Pattern 一个正则表达式经编译后的表现模式,可以理解为某正则的对应类
- Matcher 匹配检查器,根据Pattern对象作为匹配模式对字符串进行匹配检查
1、Pattern
1.1 获取Pattern对象
- static Pattern Pattern.compile(String regExp)
Pattern p = Pattern.compile("\\w+");
p.pattern(); //返回 \w+
Pattern p = Pattern.compile("\\w+");
p.pattern(); //返回 \w+
| Pattern | Pattern.compile(String regExp) | 返回正则表达式对应的Pattern对象 |
| String | pattern() | 返回Pattern对象对应的正则表达式的字符串形式 |
1.2 分隔字符串
Pattern p=Pattern.compile("\\d+");
String[] str=p.split("我的QQ是:456456我的电话是:0532214我的邮箱是:aaa@aaa.com");
//结果:str[0]="我的QQ是:" str[1]="我的电话是:" str[2]="我的邮箱是:aaa@aaa.com"
Pattern p=Pattern.compile("\\d+");
String[] str=p.split("我的QQ是:456456我的电话是:0532214我的邮箱是:aaa@aaa.com");
//结果:str[0]="我的QQ是:" str[1]="我的电话是:" str[2]="我的邮箱是:aaa@aaa.com"
| String[] | split(CharSequence input) | 符合匹配规则的字符串作为分隔符,分隔字符串并返回字符串数组 |
1.3 判断是否完整符合匹配规则
Pattern.matches("\\d+","2223");//返回true
Pattern.matches("\\d+","2223aa");//返回false,需要匹配到所有字符串才能返回true,这里aa不符
Pattern.matches("\\d+","22bb23");//返回false,需要匹配到所有字符串才能返回true,这里bb不符
Pattern.matches("\\d+","2223");//返回true
Pattern.matches("\\d+","2223aa");//返回false,需要匹配到所有字符串才能返回true,这里aa不符
Pattern.matches("\\d+","22bb23");//返回false,需要匹配到所有字符串才能返回true,这里bb不符
| boolean | matches(String regex, CharSequence input) | 判断字符串是否完整匹配对应的正则表达式 |
2、Matcher
2.1 获取Matcher对象
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("22bb23");
m.pattern(); //返回p 也就是返回该Matcher对象是由哪个Pattern对象的创建的
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("22bb23");
m.pattern(); //返回p 也就是返回该Matcher对象是由哪个Pattern对象的创建的
| Matcher | matcher(CharSequence input) | 返回一个对应Pattern的Matcher对象 |
2.2 判断匹配规则
2.2.1 完整匹配判断
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("22bb23");
m.matches(); //返回false,因为bb不能被\d+匹配,导致整个字符串匹配未成功.
Matcher m2 = p.matcher("2223");
m2.matches(); //返回true,因为\d+匹配到了整个字符串
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("22bb23");
m.matches(); //返回false,因为bb不能被\d+匹配,导致整个字符串匹配未成功.
Matcher m2 = p.matcher("2223");
m2.matches(); //返回true,因为\d+匹配到了整个字符串
| boolean | matches() | 判断对应字符串是否完全匹配正则表达式 |
2.2.2 开头匹配判断
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
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
| boolean | lookingAt() | 符合正则的子串是否位于字符串首位 |
2.2.3 子串匹配判断
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
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
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("aaa2223bb222");
while (matcher.find()) {
System.out.println("ok");
}
//会输出两次ok,第一次匹配到了2223,第二次匹配到了尾部的222
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("aaa2223bb222");
while (matcher.find()) {
System.out.println("ok");
}
//会输出两次ok,第一次匹配到了2223,第二次匹配到了尾部的222
| boolean | find() | 判断是否存在符合正则的子串,可以重复调用直到找不到符合匹配的子串为止 |
2.3 子串匹配
- start() 返回匹配子串在字符串中的索引位置
- end() 返回匹配子串的最后一个字符在字符串中的索引位置
- group() 返回匹配到的子串
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("aaa2223bb");
m.find(); //返回true,匹配2223
m.start(); //返回3
m.end(); //返回7, 返回的是2223后的位置,非索引号
m.group(); //返回2223
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("aaa2223bb");
m.find(); //返回true,匹配2223
m.start(); //返回3
m.end(); //返回7, 返回的是2223后的位置,非索引号
m.group(); //返回2223
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("aaa2223bb222");
System.out.println(matcher.find());
System.out.println(matcher.group()); //返回2223
System.out.println(matcher.group()); //返回2223
System.out.println(matcher.group()); //返回2223
//因为上述find没有再次进行操作,即此刻的子串始终指向2223,那么下面我们将find循环调用
while (matcher.find()) {
System.out.println(matcher.group() + " - " + matcher.start() + ", " + matcher.end());
}
//会得到两个输出
//2223 - 3, 7
//222 - 9, 12
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("aaa2223bb222");
System.out.println(matcher.find());
System.out.println(matcher.group()); //返回2223
System.out.println(matcher.group()); //返回2223
System.out.println(matcher.group()); //返回2223
//因为上述find没有再次进行操作,即此刻的子串始终指向2223,那么下面我们将find循环调用
while (matcher.find()) {
System.out.println(matcher.group() + " - " + matcher.start() + ", " + matcher.end());
}
//会得到两个输出
//2223 - 3, 7
//222 - 9, 12
| int | start() | 返回匹配子串在字符串中的索引位置 |
| int | end() | 返回匹配子串最后一个字符在字符串中的位置(非索引) |
| String | group() | 返回匹配到的子串 |
2.4 分组操作
Pattern pattern = Pattern.compile("([a-z]+)(\\d+)");
Matcher matcher = pattern.matcher("aaa2223bb222xxx88");
System.out.println(matcher.groupCount());
System.out.println("========");
while (matcher.find()) {
System.out.println(matcher.group());
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println("--------");
}
Pattern pattern = Pattern.compile("([a-z]+)(\\d+)");
Matcher matcher = pattern.matcher("aaa2223bb222xxx88");
System.out.println(matcher.groupCount());
System.out.println("========");
while (matcher.find()) {
System.out.println(matcher.group());
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println("--------");
}
- 第一组为aaa,对应([a-z]);
- 第二组为2223,对应(\\d+)
2
========
aaa2223
aaa
2223
--------
bb222
bb
222
--------
xxx88
xxx
88
--------
2
========
aaa2223
aaa
2223
--------
bb222
bb
222
--------
xxx88
xxx
88
--------
| int | start(int i) | 返回第 i 组匹配到的子串在字符串中的索引位置 |
| int | end(int i) | 返回第 i 组匹配子串最后一个字符在字符串中的位置(非索引号) |
| String | group(int i) | 返回匹配到的字符串中第 i 组子串 |
3、参考链接
[01] Pattern类和Matcher类的更多相关文章
- JAVA正则表达式:Pattern类与Matcher类详解(转)
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和Matcher Pattern 一个Pattern是一个正则表达式经编译后的表 ...
- 正则表达式:Pattern类与Matcher类详解
一.捕获组的概念 捕获组可以通过从左到右计算其开括号来编号,编号是从1 开始的.例如,在表达式((A)(B(C)))中,存在四个这样的组: 1 ((A)(B(C))) 2 (A) 3 ...
- JAVA正则表达式:Pattern类与Matcher类详解
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和Matcher Pattern 一个Pattern是一个正则表达式经编译后的表 ...
- Java正则表达式:Pattern类和Matcher类
一.捕获组的概念 捕获组可以通过从左到右计算其开括号来编号,编号是从1 开始的.例如,在表达式 ((A)(B(C)))中,存在四个这样的组: 1 ((A)(B(C))) 2 (A) 3 ...
- [转] JAVA正则表达式:Pattern类与Matcher类详解(转)
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和 Matcher Pattern 一个Pattern是一个正则表达式经编译后的 ...
- java.util.regex包下的Pattern类和Matcher类的使用总结
一.介绍 Java正则表达式通过java.util.regex包下的Pattern类与Matcher类实现1.Pattern类用于创建一个正则表达式,也可以说创建一个匹配模式,它的构造方法是私有的,不 ...
- 详解Pattern类和Matcher类
java正则表达式通过java.util.regex包下的Pattern类与Matcher类实现(建议在阅读本文时,打开java API文档,当介绍到哪个方法时,查看java API中的方法说明,效果 ...
- 正则表达式中Pattern类、Matcher类和matches()方法简析
1.简介: java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. 它包括两个类:Pattern和Matcher . Pattern: 一个Pattern是一 ...
- 14-01 Java matches类,Pattern类,matcher类
Pattern类 正则表达式常见规则 A:字符 x 字符 x.举例:'a'表示字符a \\ 反斜线字符. \n 新行(换行)符 ('\u000A') \r 回车符 ('\u000D') B:字符类 [ ...
随机推荐
- PHP 常见工厂设计模式
一.工厂模式 是一种类,它具有为您创建对象的某些方法.您可以使用工厂类创建对象,而不直接使用 new.这样,如果您想要更改所创建的对象类型,只需更改该工厂即可.使用该工厂的所有代码会自动更改. 下面代 ...
- meta标签的name和http-equiv属性
META标签是HTML语言HEAD区的一个辅助性标签,它位于HTML文档头部的<HEAD>标记和<TITLE>标记之间,它提供用户不可见的信息.META标签有两个重要的属性:H ...
- java protected修饰符理解
Protected类型可以在本包和子类中访问的含义.在本包中访问,顾名思义,可以在定义类的包中的任何地方申请一个包含protected修饰符的域和方法的类的对象,并通过这个对象访问被protected ...
- 安装mvn,jdk,rocketmq
附一个rocketmq各语言客户端仓库:https://github.com/apache/incubator-rocketmq-externals,我用go客户端,但是master分支的go没有pr ...
- PHP迭代与递归实现无限级分类
无限级分类是开发中常见的情况,因此本文对常见的无限极分类算法进行总结归纳. 1.循环迭代实现 $arr = [ 1=>['id'=>1,'name'=>'父1','father'=& ...
- Markov不等式,Chebyshev不等式
在切诺夫界的证明中用到了Markov不等式,证明于此~顺便把Chebyshev不等式也写上了
- Vue-上拉加载与下拉刷新(mint-ui:loadmore)一个页面使用多个上拉加载后冲突问题
所遇问题: 该页面为双选项卡联动,四个部分都需要上拉加载和下拉刷新功能,使用的mint-ui的loadmore插件,分别加上上拉加载后,只有最后一个的this.$refs.loadmore.onTop ...
- Mysql数据库索引
今天,我们来讲讲Mysql数据库的索引的一些东西,想必大家都知道索引能干吗?必然是查找数据表的时候,查找的速度快啊,尤其是那些几百万行的数据库,不建立索引,你是想考验用户的耐心吗?Mysql有多种存储 ...
- postgresql如何维护WAL日志/归档日志
WAL日志介绍 wal全称是write ahead log,是postgresql中的online redo log,是为了保证数据库中数据的一致性和事务的完整性.而在PostgreSQL 7中引入的 ...
- vue-router的两种模式的区别
众所周知,vue-router有两种模式,hash模式和history模式,这里来谈谈两者的区别. ### hash模式 hash模式背后的原理是`onhashchange`事件,可以在`window ...