栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

正则表达式在Java中的命名组

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

正则表达式在Java中的命名组

每个名称只能具有一个命名组(你并不总是可以控制!),并且不能将它们用于正则表达式内递归。

注意:你可以在Perl和PCRE正则表达式中找到真正的正则表达式递归示例,如Regexp Power,PCRE规范和带有平衡括号的匹配字符串幻灯片中所述)

例:

String:

"TEST 123"

RegExp::

"(?<login>\w+) (?<id>\d+)"

Access

matcher.group(1) ==> TESTmatcher.group("login") ==> TESTmatcher.name(1) ==> login

Replace

matcher.replaceAll("aaaaa_$1_sssss_$2____") ==> aaaaa_TEST_sssss_123____matcher.replaceAll("aaaaa_${login}_sssss_${id}____") ==> aaaaa_TEST_sssss_123____ 
public final class Pattern    implements java.io.Serializable{[...]        private Node group0() {        boolean capturingGroup = false;        Node head = null;        Node tail = null;        int save = flags;        root = null;        int ch = next();        if (ch == '?') { ch = skip(); switch (ch) { case '<':   // (?<xxx)  look behind or group name     ch = read();     int start = cursor;[...]     // test forGroupName     int startChar = ch;     while(ASCII.isWord(ch) && ch != '>') ch=read();     if(ch == '>'){         // valid group name         int len = cursor-start;         int[] newtemp = new int[2*(len) + 2];         //System.arraycopy(temp, start, newtemp, 0, len);         StringBuilder name = new StringBuilder();         for(int i = start; i< cursor; i++){  name.append((char)temp[i-1]);         }         // create Named group         head = createGroup(false);         ((GroupTail)root).name = name.toString();         capturingGroup = true;         tail = root;         head.next = expr(tail);         break;     }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/374833.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号