正则表达式子组深入解析
1. 理解子组
正则表达式中,除了组之外,还可以有子组。子组是大整体中的较小分组,它们通过括号与原始组以及其他子组分隔开来。
1.1 子组示例
为了能明确引用数字 \d ,可以将模式修改为 \w(\d) 。这里, \w\d 是组 (0) , (\d) 是组 (1) 。以下是一个 Java 示例代码,展示了如何使用子组提取与数字 \d 匹配的候选部分:
import java.util.regex.*;
public class SimpleSubGroupExample{
public static void main(String args[]){
//the original pattern is always group 0
Pattern p = Pattern.compile("\\w(\\d)");
String candidate = "A9 is my favorite";
//if there is a match, extract the parts that
//match.
Matcher matcher = p.matcher(candidate);
if (matcher.find()){
//Ext
超级会员免费看
订阅专栏 解锁全文
51

被折叠的 条评论
为什么被折叠?



