Java正则表达式中的捕获组的概念及相关API使用

简介: 要弄清这三个方法,首先要弄清Java正则表达式中的捕获组的概念。捕获组也就是Pattern中以括号对“()”分割出的子Pattern。至于为什么要用捕获组呢,主要是为了能找出在一次匹配中你更关心的部分。

要弄清这三个方法,首先要弄清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

 

 


 

相关文章
|
1月前
|
Java 编译器 Go
【Java】(5)方法的概念、方法的调用、方法重载、构造方法的创建
Java方法是语句的集合,它们在一起执行一个功能。方法是解决一类问题的步骤的有序组合方法包含于类或对象中方法在程序中被创建,在其他地方被引用方法的优点使程序变得更简短而清晰。有利于程序维护。可以提高程序开发的效率。提高了代码的重用性。方法的名字的第一个单词应以小写字母作为开头,后面的单词则用大写字母开头写,不使用连接符。例如:addPerson。这种就属于驼峰写法下划线可能出现在 JUnit 测试方法名称中用以分隔名称的逻辑组件。
196 4
|
2月前
|
Java API 数据处理
Java新特性:使用Stream API重构你的数据处理
Java新特性:使用Stream API重构你的数据处理
|
2月前
|
Java 大数据 API
Java Stream API:现代集合处理与函数式编程
Java Stream API:现代集合处理与函数式编程
237 100
|
2月前
|
Java API 数据处理
Java Stream API:现代集合处理新方式
Java Stream API:现代集合处理新方式
267 101
|
2月前
|
并行计算 Java 大数据
Java Stream API:现代数据处理之道
Java Stream API:现代数据处理之道
241 101
|
2月前
|
安全 Java API
使用 Java 构建强大的 REST API 的四个基本技巧
本文结合探险领域案例,分享Java构建REST API的四大核心策略:统一资源命名、版本控制与自动化文档、安全防护及标准化异常处理,助力开发者打造易用、可维护、安全可靠的稳健API服务。
196 2
|
2月前
|
存储 数据可视化 Java
Java Stream API 的强大功能
Java Stream API 是 Java 8 引入的重要特性,它改变了集合数据的处理方式。通过声明式语法,开发者可以更简洁地进行过滤、映射、聚合等操作。Stream API 支持惰性求值和并行处理,提升了代码效率和可读性,是现代 Java 开发不可或缺的工具。
Java Stream API 的强大功能
|
3月前
|
安全 Java API
Java日期时间API:从Date到Java.time
本文深入解析了Java 8中引入的全新日期时间API,涵盖LocalDate、LocalTime、LocalDateTime、ZonedDateTime等核心类的使用,以及时间调整、格式化、时区处理和与旧API的互操作。通过实例对比,展示了新API在可变性、线程安全与易用性方面的显著优势,并提供迁移方案与实战技巧,助你掌握现代Java时间处理的最佳实践。
|
3月前
|
存储 NoSQL Java
Java Stream API:集合操作与并行处理
Stream API 是 Java 8 提供的集合处理工具,通过声明式编程简化数据操作。它支持链式调用、延迟执行和并行处理,能够高效实现过滤、转换、聚合等操作,提升代码可读性和性能。
|
3月前
|
存储 Java API
Java Stream API:现代数据处理之道
Java Stream API:现代数据处理之道
366 188
下一篇
oss云网关配置