Java正则表达式中的捕获组的概念及相关API使用
要弄清这三个方法,首先要弄清Java正则表达式中的捕获组的概念。捕获组也就是Pattern中以括号对“()”分割出的子Pattern。至于为什么要用捕获组呢,主要是为了能找出在一次匹配中你更关心的部分。
捕获组可以通过从左到右计算其开括号来编号。例如,在表达式 "(x)(y\\w*)(z)" 中,存在三个这样的组:
1. x
2. y\\w*
3. z
组零始终代表整个表达式。
之所以这样命名捕获组是因为在匹配中,保存了与这些组匹配的输入序列的每个子序列。捕获的子序列稍后可以通过 Back 引用在表达式中使用,也可以在匹配操作完成后从匹配器获取。
以 (?) 开头的组是纯的非捕获 组,它不捕获文本,也不针对组合计进行计数。
Example:
package pattern; import java.util.regex.Matcher;
import java.util.regex.Pattern; public class testRegex {
public static void main(String[] args) {
String regex = "(x)(y\\w*)(z)"; String input = "exy123z,xy456z";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(input); while (m.find()) {
System.out.println(m.group(2));
}
}
}
运行结果:
y123
y456
http://blog.163.com/xiejunshlh@126/blog/static/1662603142011219625597/
import java.util.regex.Matcher;
import java.util.regex.Pattern; /*2015-9-9*/
public class RegexDemo {
public static void main(String[] args) {
String regex = "(T_V)([\\d])|(xx)+";
String source = "T_V123,TX_V1,T_V234";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(source);
System.out.println("是否匹配:"+matcher.matches());
if (matcher.find()) {
for (int i = 0; i <= matcher.groupCount(); i++) {
String group = matcher.group(i);
System.out.println("number " + i + ":" + group+"; Is null:"+(group==null));
}
}
} }
输出:
是否匹配:false
number 0:T_V1; Is null:false
number 1:T_V; Is null:false
number 2:1; Is null:false
number 3:null; Is null:true
解析:
(1)Matcher.matches()返回值为false,是因为matches是正则表达式和整个字符串进行匹配
API:
public boolean matches()
Attempts to match the entire region against the pattern.
If the match succeeds then more information can be obtained via the start, end, and group methods.
Returns:
true if, and only if, the entire region sequence matches this matcher's pattern 注:
matches
public boolean matches()
尝试将整个区域与模式匹配。
如果匹配成功,则可以通过 start、end 和 group 方法获取更多信息。
返回:
当且仅当整个区域序列匹配此匹配器的模式时才返回 true。
(2)Matcher.find() 源字符串中任一个子序列满足条件即返回true
public boolean find()
Attempts to find the next subsequence of the input sequence that matches the pattern.
This method starts at the beginning of this matcher's region, or,
if a previous invocation of the method was successful and the matcher has not since been reset,
at the first character not matched by the previous match.
If the match succeeds then more information can be obtained via the start, end, and group methods.
Returns:
true if, and only if, a subsequence of the input sequence matches this matcher's pattern
注:
find
public boolean find()
尝试查找与该模式匹配的输入序列的下一个子序列。
此方法从匹配器区域的开头开始,如果该方法的前一次调用成功了并且从那时开始匹配器没有被重置,则从以前匹配操作没有匹配的第一个字符开始。
如果匹配成功,则可以通过 start、end 和 group 方法获取更多信息。
返回:
当且仅当输入序列的子序列匹配此匹配器的模式时才返回 true。
正则表达式匹配中文
说明:
(1)现在网上大多数用于判断中文字符的是 \u4E00-\u9FA5 这个范围是只是“中日韩统一表意文字”这个区间,但这不是全部,
如果要全部包含,则还要他们的扩展集、部首、象形字、注间字母等等; 具体可以查看unicode中简体中文编码
(2) "[一-龥]";是查出的\u4E00-\u9FA5对应的中文。具体uniocde2中文进行查询
public class StringRegexDemo {
public static void main(String[] args) {
isMatch("唐", "[\u4E00-\u9FA5]");
isMatch("晴", "[一-龥]");
} protected static void isMatch(String source, String regexStr) {
boolean result = source.matches(regexStr);
System.out.println(result);
}
}
输出:
true
true
http://blog.csdn.net/xyls12345/article/details/23942533
Java正则表达式中的捕获组的概念及相关API使用的更多相关文章
- PHP正则中的捕获组与非捕获组
今天遇到一个正则匹配的问题,忽然翻到有捕获组的概念,手册上也是一略而过,百度时无意翻到C#和Java中有对正则捕获组的特殊用法,搜索关键词有PHP时竟然没有相关内容,自己试了一下,发现在PHP中也是可 ...
- 关于 Java正则表达式中的Possessive数量修饰词的理解
关于 Java正则表达式中的Possessive数量修饰词的理解 正则表达式对于数量限定符如 ?, + , *, {n, m} 的匹配默认是贪婪模式,比如: a.*b 匹配 acbab 的结果是 ...
- JBPM4.4_核心概念与相关API
1. 核心概念与相关API(Service API) 1.1. 概念:Process definition, process instance , execution 1.1.1. Process ...
- 关于java多线程中异常捕获的理解
在java多线程程序中,所有线程都不允许抛出未捕获的checked exception(比如sleep时的InterruptedException),也就是说各个线程需要自己把自己的checked e ...
- Java 正则表达式 中的 任意字符
原来正则表达式中的"."代表的是除换行以外的任意字符,如果要真正代表任意字符,需要把换行符也加进去,但是经过测试"[.\\n]"不生效,可以使用"\\ ...
- JAVA正则表达式中如何匹配反斜杠 \
有时候我们需要匹配反斜杠,你可能会把对应的正则表达式写成 "\\" 然后可能会有如下输出: Exception in thread "main" java.ut ...
- [转]java异常中Exception捕获不到的异常
一 概念 众所周知java提供了丰富的异常类,这些异常类之间有严格的集成关系,分类为 父类Throwable Throwable的两个子类Error和Exception Exception的两个子类C ...
- java异常中Exception捕获不到的异常
一 概念 众所周知java提供了丰富的异常类,这些异常类之间有严格的集成关系,分类为 父类Throwable Throwable的两个子类Error和Exception Exception的两个子类C ...
- java正则表达式中的POSIX 字符类和Unicode 块和类别的类介绍
假如现在有一个需求,要你用Java语言来匹配出一个文本里面的所有(英文半角)标点符号,你会怎么写呢?我想大多数人应该是把这些符号都罗列出来, 如: !"#$%&'()*+,-./:; ...
随机推荐
- Plain old data structure(POD)
Plain old data structure, 缩写为POD, 是C++语言的标准中定义的一类数据结构,POD适用于需要明确的数据底层操作的系统中.POD通常被用在系统的边界处,即指不同系统之间只 ...
- JS实例(一)
一:单选按钮,选择同意,提交变为可用,反正提交不可用: HTML里面代码: <form id="f1" name="f1"> <input t ...
- Spring AOP体系学习总结:
二.Spring AOP体系学习总结: 要理解AOP整体的逻辑需要理解一下Advice,Pointcut,Advisor的概念以及他们的关系. Advice是为Spring Bean提供增强逻辑的接口 ...
- c#不重复的排序方法
public int getRandom(int num) { Thread.Sleep(5); // Random ro = new Random(unchecked((int)DateTime.N ...
- python matplotlib.plot画图显示中文乱码的问题
在matplotlib.plot生成的统计图表中,中文总是无法正常显示.在网上也找了些资料,说是在程序中指定字体文件,不过那样的话需要对plot进行很多设置,而且都是说的设置坐标轴标题为中文,有时候图 ...
- java 子类重写父类的方法应注意的问题
若想实现一个合格重写方法,而不是重载,那么必须同时满足下面的要求! A.重写规则之一: 重写方法不能比被重写方法限制有更严格的访问级别.(但是可以更广泛,比如父类方法是包访问权限,子类的重写方法 ...
- 数据库ACID、隔离级别与MVCC
首先需要明确事务的概念:一组原子性的SQL查询,如果数据库引擎能够成功的对数据库应用该组查询的全部语句,那么就执行该组语句,否则所有语句都不执行. 事务有ACID四个特性,即: 原子性:一个事务是一个 ...
- C#获取数组的行和列数程序代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- JavaScript中Ajax的get和post请求
AJAX = 异步 JavaScript和XML(Asynchronous JavaScript and XML) 作用:在不重新加载整个网页的情况下,对网页的某部分进行更新. 两种请求方式: 1 ...
- hdoj (1162) 最小生成树
Problem B Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...