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语言来匹配出一个文本里面的所有(英文半角)标点符号,你会怎么写呢?我想大多数人应该是把这些符号都罗列出来, 如: !"#$%&'()*+,-./:; ...
随机推荐
- 使用选择器语法来查找元素 - 你想使用类似于CSS或jQuery的语法来查找和操作元素
http://www.open-open.com/jsoup/selector-syntax.htm
- Spring Mvc和Mybatis的多数据库访问配置过程
Spring Mvc 加Mybatis的多数据库访问源配置访问过程如下: 在applicationContext.xml进行配置 <?xml version="1.0" en ...
- Plain old data structure(POD)
Plain old data structure, 缩写为POD, 是C++语言的标准中定义的一类数据结构,POD适用于需要明确的数据底层操作的系统中.POD通常被用在系统的边界处,即指不同系统之间只 ...
- Linux中Curl命令couldn't connect to host解决方案 php操作Curl(http,https)无法获取远程数据解决方案
本人在做百度账户第三方登录接口,获取百度token,利用php操作curl post方式发送请求token,出现couldn't connect to host错误.经过调试测试,最后终于成功.回头写 ...
- 使用html5兼容低版本浏览器
因为html5 新出的一些语义化的标签,在低版本浏览器下不能识别,举个例子,比如你写了一个 header 标签中,写了一段文本,在低版本浏览器下,肯定是能看到的,但是,那是他是不认识 header标签 ...
- Using load balance for thrift servers
Software load balance .Nginx(http://nginx.org) 1.Install nginx download source code from http://ngin ...
- CSS之关于clearfix--清除浮动
一,什么是.clearfix 你只要到Google或者Baidu随便一搜"css清除浮动",就会发现很多网站都讲到"盒子清除内部浮动时可以用到.clearfix" ...
- sqlserver2005唯一性约束
[转载]http://blog.163.com/rihui_7/blog/static/21228514320136193392749/ 1.设置字段为主键就是一种唯一性约束的方法,如 int p ...
- JavaEE web.xml 中ContextLoaderListener的解析
ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在 ...
- SGU 296.Sasha vs. Kate(贪心)
题意: 给出长度为n(<=1000)的一个数.输出删掉k个数字后的最大值. Solution: 简单贪心. s[i]代表数字s的第i位. 从前往后第一个满足s[i]>s[i-1]的位置,最 ...