正则表达式参考:平衡组、递归和子常式正则表达式参考:平衡组、递归和子常式正则表达式参考:平衡组、递归和子常式正则表达式参考:平衡组、递归和子常式
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
正则表达式参考:平衡组、递归和子常式
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式参考 » 正则表达式参考:平衡组、递归和子常式

正则表达式参考
简介
目录
快速参考
字符
基本功能
字符类别
速记
锚点
字词边界
量词
Unicode
捕获组和反向引用
命名组和反向引用
特殊组
模式修改器
递归和平衡组
替换参考
字符
符合文本和反向引用
内容和大小写转换
条件式
本网站更多信息
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

正则表达式参考:平衡组、递归和子常式

功能语法说明范例.NET Java Perl PCRE PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE POSIX BRE POSIX ERE GNU BRE GNU ERE Oracle XML XPath
平衡组 (?<capture-subtract>regex) 其中「capture」和「subtract」是群组名称,而「regex」是任何正则表达式 「subtract」名称必须用于正则表达式中其他位置的捕获组名称。如果这个群组已截取尚未减掉的符合项,则平衡组会从「subtract」中减去一个截取,尝试符合「regex」,并将其符合项保存在群组「capture」中。如果省略「capture」,则会发生相同情况,但不会保存符合项。如果省略「regex」,则平衡组会成功,但不会前进字符串。如果群组「subtract」没有符合项可以减去,则平衡组会无法符合,无论是否指定「regex」。 ^(?<l>\w)+\w?
(\k<l>(?<-l>))+
(?(l)(?!))$
符合任何回文单字
YESnononononononononononononononononononononono
平衡组 (?'capture-subtract'regex) 其中「capture」和「subtract」是群组名称,而「regex」是任何正则表达式 「subtract」名称必须用于正则表达式中其他位置的捕获组名称。如果这个群组已截取尚未减掉的符合项,则平衡组会从「subtract」中减去一个截取,尝试符合「regex」,并将其符合项保存在群组「capture」中。如果省略「capture」,则会发生相同情况,但不会保存符合项。如果省略「regex」,则平衡组会成功,但不会前进字符串。如果群组「subtract」没有符合项可以减去,则平衡组会无法符合,无论是否指定「regex」。 ^(?'l'\w)+\w?
(\k'l'(?'-l'))+
(?(l)(?!))$
符合任何回文单字
YESnononononononononononononononononononononono
递归 (?R) 整个正则表达式的递归。 a(?R)?z 符合 az、aazz、aaazzz 等。 nono5.10YESYESYESYESYESnonononononoECMA
1.42–1.83
nononononononono
递归 (?0) 整个正则表达式的递归。 a(?0)?z 符合 az、aazz、aaazzz 等。 nono5.10YESYESYESYESYESnonononononoECMA
1.42–1.83
nononononononono
递归 \g<0> 整个正则表达式的递归。 a\g<0>?z 符合 az、aazz、aaazzz 等。 nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
递归 \g'0' 整个正则表达式的递归。 a\g'0'?z 符合 az、aazz、aaazzz 等。 nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
子常式调用 (?1) 其中 1 是捕获组的编号 捕获组的递归或子常式调用至捕获组。 a(b(?1)?y)z 符合 abyz、abbyyz、abbbyyyz 等。 no仅 45.10YESYESYESYESYESnonononononoECMA
1.42–1.83
nononononononono
子常式调用 \g<1>,其中 1 是捕获组的编号 捕获组的递归或子常式调用至捕获组。 a(b\g<1>?y)z 符合 abyz、abbyyz、abbbyyyz 等 nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
子常式调用 \g'1',其中 1 是捕获组的编号 捕获组的递归或子常式调用至捕获组。 a(b\g'1'?y)z 符合 abyz、abbyyz、abbbyyyz 等 nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
相对子常式调用 (?-1),其中 -1 是负整数 递归或子常式调用到捕获组,可通过计算从右到左从子常式调用开始,指定数量命名或编号捕获组的打开括号来找出。 a(b(?-1)?y)z 符合 abyz、abbyyz、abbbyyyz 等 nono5.107.2YES5.2.4YESYESnonononononoECMA
1.42–1.83
nononononononono
相对子常式调用 \g<-1>,其中 -1 是负整数 递归或子常式调用到捕获组,可通过计算从右到左从子常式调用开始,指定数量命名或编号捕获组的打开括号来找出。 a(b\g<-1>?y)z 符合 abyz、abbyyz、abbbyyyz 等 nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
相对子常式调用 \g'-1',其中 -1 是负整数 递归或子常式调用到捕获组,可通过计算从右到左从子常式调用开始,指定数量命名或编号捕获组的打开括号来找出。 a(b\g'-1'?y)z 符合 abyz、abbyyz、abbbyyyz 等 nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
前向子常式调用 (?+1),其中 +1 是正整数 递归或子常式调用到捕获组,可通过计算从左到右从子常式调用开始,指定数量命名或编号捕获组的打开括号来找出。 (?+1)x([ab]) 符合 axa、axb、bxa 和 bxb nono5.107.2YES5.2.4YESYESnonononononoECMA
1.42–1.83
nononononononono
前向子常式调用 \g<+1>,其中 +1 为正整数 递归或子常式调用到捕获组,可通过计算从左到右从子常式调用开始,指定数量命名或编号捕获组的打开括号来找出。 \g<+1>x([ab]) 符合 axa、axb、bxa 和 bxb nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
前向子常式调用 \g'+1',其中 +1 为正整数 递归或子常式调用到捕获组,可通过计算从左到右从子常式调用开始,指定数量命名或编号捕获组的打开括号来找出。 \g'+1'x([ab]) 符合 axa、axb、bxa 和 bxb nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
命名子常式调用 (?&name),其中「name」为捕获组的名称 捕获组的递归或子常式调用至捕获组。 a(?<x>b(?&x)?y)z 符合 abyz、abbyyz、abbbyyyz 等 nono5.107.0YES5.2.2YESYESnonononononoECMA
1.42–1.83
nononononononono
命名子常式调用 (?P>name),其中「name」为捕获组的名称 捕获组的递归或子常式调用至捕获组。 a(?P<x>b(?P>x)?y)z 符合 abyz、abbyyz、abbbyyyz 等 nono5.10YESYESYESYESYESnonononononoECMA
1.42–1.83
nononononononono
命名子常式调用 \g<name>,其中「name」为捕获组的名称 捕获组的递归或子常式调用至捕获组。 a(?<x>b\g<x>?y)z 符合 abyz、abbyyz、abbbyyyz 等 nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
命名子常式调用 \g'name',其中「name」为捕获组的名称 捕获组的递归或子常式调用至捕获组。 a(?'x'b\g'x'?y)z 符合 abyz、abbyyz、abbbyyyz 等 nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
子常式定义 (?(DEFINE)regex),其中「regex」为任意正则表达式 DEFINE 群组不参与比对进程。可以在 DEFINE 群组内对捕获组进行子常式调用。 (?(DEFINE)([ab]))
x(?1)y(?1)z
符合 xayaz、xaybz、xbyaz 和 xbybz
nono5.107.0YES5.2.2YESYESnonononononoECMA
1.42–1.83
nononononononono
子常式调用截取 使用 Ruby 风格 \g 语法进行子常式调用 对捕获组的子常式调用会让该捕获组保存子常式调用期间符合的文本。 当 ([ab])\g'1' 符合 ab 时,第一个捕获组会在符合后保存 b。 不适用不适用不适用nonononono不适用不适用不适用不适用1.9不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用
子常式调用截取 使用 \g 以外的语法进行子常式调用 对捕获组的子常式调用会让该捕获组保存子常式调用期间符合的文本。 当 ([ab])(?1) 符合 ab 时,第一个捕获组会在符合后保存 b。 不适用nonononononono不适用不适用不适用不适用不适用不适用no不适用不适用不适用不适用不适用不适用不适用不适用
递归隔离捕获组 任何递归或子常式调用 每个子常式调用都有自己的捕获组独立保存空间。调用中的反向引用无法看到调用之前的符合文本,调用之后的反向引用也无法看到调用中的符合文本。递归中的反向引用无法看到其他递归层级的符合文本。 (a)(\1)(?2) 永远不会符合任何东西,因为 \1 总会在 (?2) 进行的调用中失败。 不适用no5.10–5.18nonononono不适用不适用不适用不适用no不适用no不适用不适用不适用不适用不适用不适用不适用不适用
递归还原捕获组 使用 Ruby 风格 \g 语法的递归或子常式调用 当正则表达式引擎退出递归或子常式调用时,它会将所有捕获组还原为进入递归或子常式调用之前符合的文本。 当 (a)(([bc])\1)\g'2' 符合 abaca 时,第三个群组会在符合后保存 b 不适用不适用不适用7.7YES5.2.7YESYES不适用不适用不适用不适用no不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用
递归还原捕获组 使用 \g 以外的语法进行递归或子常式调用 当正则表达式引擎退出递归或子常式调用时,它会将所有捕获组还原为进入递归或子常式调用之前符合的文本。 当 (a)(([bc])\1)(?2) 比对 abaca 时,第三个群组会在比对后保存 b 不适用no5.10YESYESYESYESYES不适用不适用不适用不适用不适用不适用ECMA
1.42–1.83
不适用不适用不适用不适用不适用不适用不适用不适用
递归不会孤立或还原捕获组 使用 Ruby 风格 \g 语法的递归或子常式调用 递归和子常式调用不会对捕获组给予任何特殊处理,除了子常式调用会截取之外。反向引用总是会看到每个捕获组最近比对到的文本,无论它们是否在同层递归中。 当 (a)(([bc])\1)\g'2' 比对 abaca 时,第三个群组会在比对后保存 c 不适用不适用不适用nonononono不适用不适用不适用不适用1.9不适用不适用不适用不适用不适用不适用不适用不适用不适用不适用
递归不会孤立或还原捕获组 使用 \g 以外的语法进行递归或子常式调用 递归和子常式调用不会对捕获组给予任何特殊处理,除了子常式调用会截取之外。反向引用总是会看到每个捕获组最近比对到的文本,无论它们是否在同层递归中。 当 (a)(([bc])\1)(?2) 比对 abaca 时,第三个群组会在比对后保存 c 不适用仅 4nononononono不适用不适用不适用不适用不适用不适用no不适用不适用不适用不适用不适用不适用不适用不适用
递归是原子的 使用 (?P>…) 的递归或子常式调用 递归和子常式调用是原子的。一旦正则表达式引擎退出它们,它就不会回溯到它们中尝试递归或子常式调用的不同排列。 (a+)(?P>1)(?P>1) 永远无法比对任何东西,因为第一个 (?P>1) 比对所有剩下的 a,而当第二个失败时,正则表达式引擎不会回溯到第一个 (?P>1) 不适用不适用no6.510.00–10.235.1.3–7.2.34YES2.14.0–3.6.3不适用不适用不适用不适用不适用不适用no不适用不适用不适用不适用不适用不适用不适用不适用
递归是原子的 使用 (?P>0) 以外的语法递归整个正则表达式 整个正则表达式的递归是原子的。一旦正则表达式引擎退出递归,它就不会回溯到它中尝试递归的不同排列。 aa$|a(?R)a|a 在递归为原子的情况下,会在 aaa 中比对 a;否则它会比对整个字符串。 不适用不适用no6.5YES5.1.3–7.2.34YESYES不适用不适用不适用不适用no不适用ECMA
1.42–1.83
不适用不适用不适用不适用不适用不适用不适用不适用
递归是原子的 使用语法调用子常式,而非 (?P>…) 子常式调用是原子性的。一旦正则表达式引擎从中退出,它就不会回溯到其中尝试子常式调用的不同排列。 (a+)(?1)(?1) 永远无法配对任何东西,因为第一个 (?1) 配对所有剩下的 a,而当第二个失败时,正则表达式引擎不会回溯到第一个 (?1) 不适用仅 4no6.510.00–10.235.1.3–7.2.34YES2.14.0–3.6.3不适用不适用不适用不适用no不适用no不适用不适用不适用不适用不适用不适用不适用不适用
功能语法说明范例.NET Java Perl PCRE PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE POSIX BRE POSIX ERE GNU BRE GNU ERE Oracle XML XPath
正規表示式參考:平衡群組、遞迴和子常式
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式參考 » 正規表示式參考:平衡群組、遞迴和子常式

正規表示式參考
簡介
目錄
快速參考
字元
基本功能
字元類別
速記
錨點
字詞邊界
量詞
Unicode
擷取群組和反向參照
命名群組和反向參照
特殊群組
模式修改器
遞迴和平衡群組
替換參考
字元
符合文字和反向參照
內容和大小寫轉換
條件式
本網站更多資訊
簡介
正規表示式快速入門
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

正規表示式參考:平衡群組、遞迴和子常式

功能語法說明範例.NET Java Perl PCRE PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE POSIX BRE POSIX ERE GNU BRE GNU ERE Oracle XML XPath
平衡群組 (?<capture-subtract>regex) 其中「capture」和「subtract」是群組名稱,而「regex」是任何正規表示式 「subtract」名稱必須用於正規表示式中其他位置的擷取群組名稱。如果這個群組已擷取尚未減掉的符合項,則平衡群組會從「subtract」中減去一個擷取,嘗試符合「regex」,並將其符合項儲存在群組「capture」中。如果省略「capture」,則會發生相同情況,但不會儲存符合項。如果省略「regex」,則平衡群組會成功,但不會前進字串。如果群組「subtract」沒有符合項可以減去,則平衡群組會無法符合,無論是否指定「regex」。 ^(?<l>\w)+\w?
(\k<l>(?<-l>))+
(?(l)(?!))$
符合任何迴文單字
YESnononononononononononononononononononononono
平衡群組 (?'capture-subtract'regex) 其中「capture」和「subtract」是群組名稱,而「regex」是任何正規表示式 「subtract」名稱必須用於正規表示式中其他位置的擷取群組名稱。如果這個群組已擷取尚未減掉的符合項,則平衡群組會從「subtract」中減去一個擷取,嘗試符合「regex」,並將其符合項儲存在群組「capture」中。如果省略「capture」,則會發生相同情況,但不會儲存符合項。如果省略「regex」,則平衡群組會成功,但不會前進字串。如果群組「subtract」沒有符合項可以減去,則平衡群組會無法符合,無論是否指定「regex」。 ^(?'l'\w)+\w?
(\k'l'(?'-l'))+
(?(l)(?!))$
符合任何迴文單字
YESnononononononononononononononononononononono
遞迴 (?R) 整個正規表示式的遞迴。 a(?R)?z 符合 az、aazz、aaazzz 等。 nono5.10YESYESYESYESYESnonononononoECMA
1.42–1.83
nononononononono
遞迴 (?0) 整個正規表示式的遞迴。 a(?0)?z 符合 az、aazz、aaazzz 等。 nono5.10YESYESYESYESYESnonononononoECMA
1.42–1.83
nononononononono
遞迴 \g<0> 整個正規表示式的遞迴。 a\g<0>?z 符合 az、aazz、aaazzz 等。 nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
遞迴 \g'0' 整個正規表示式的遞迴。 a\g'0'?z 符合 az、aazz、aaazzz 等。 nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
子常式呼叫 (?1) 其中 1 是擷取群組的編號 擷取群組的遞迴或子常式呼叫至擷取群組。 a(b(?1)?y)z 符合 abyz、abbyyz、abbbyyyz 等。 no僅 45.10YESYESYESYESYESnonononononoECMA
1.42–1.83
nononononononono
子常式呼叫 \g<1>,其中 1 是擷取群組的編號 擷取群組的遞迴或子常式呼叫至擷取群組。 a(b\g<1>?y)z 符合 abyz、abbyyz、abbbyyyz 等 nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
子常式呼叫 \g'1',其中 1 是擷取群組的編號 擷取群組的遞迴或子常式呼叫至擷取群組。 a(b\g'1'?y)z 符合 abyz、abbyyz、abbbyyyz 等 nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
相對子常式呼叫 (?-1),其中 -1 是負整數 遞迴或子常式呼叫到擷取群組,可透過計算從右到左從子常式呼叫開始,指定數量命名或編號擷取群組的開啟括號來找出。 a(b(?-1)?y)z 符合 abyz、abbyyz、abbbyyyz 等 nono5.107.2YES5.2.4YESYESnonononononoECMA
1.42–1.83
nononononononono
相對子常式呼叫 \g<-1>,其中 -1 是負整數 遞迴或子常式呼叫到擷取群組,可透過計算從右到左從子常式呼叫開始,指定數量命名或編號擷取群組的開啟括號來找出。 a(b\g<-1>?y)z 符合 abyz、abbyyz、abbbyyyz 等 nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
相對子常式呼叫 \g'-1',其中 -1 是負整數 遞迴或子常式呼叫到擷取群組,可透過計算從右到左從子常式呼叫開始,指定數量命名或編號擷取群組的開啟括號來找出。 a(b\g'-1'?y)z 符合 abyz、abbyyz、abbbyyyz 等 nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
前向子常式呼叫 (?+1),其中 +1 是正整數 遞迴或子常式呼叫到擷取群組,可透過計算從左到右從子常式呼叫開始,指定數量命名或編號擷取群組的開啟括號來找出。 (?+1)x([ab]) 符合 axa、axb、bxa 和 bxb nono5.107.2YES5.2.4YESYESnonononononoECMA
1.42–1.83
nononononononono
前向子常式呼叫 \g<+1>,其中 +1 為正整數 遞迴或子常式呼叫到擷取群組,可透過計算從左到右從子常式呼叫開始,指定數量命名或編號擷取群組的開啟括號來找出。 \g<+1>x([ab]) 符合 axa、axb、bxa 和 bxb nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
前向子常式呼叫 \g'+1',其中 +1 為正整數 遞迴或子常式呼叫到擷取群組,可透過計算從左到右從子常式呼叫開始,指定數量命名或編號擷取群組的開啟括號來找出。 \g'+1'x([ab]) 符合 axa、axb、bxa 和 bxb nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
命名子常式呼叫 (?&name),其中「name」為擷取群組的名稱 擷取群組的遞迴或子常式呼叫至擷取群組。 a(?<x>b(?&x)?y)z 符合 abyz、abbyyz、abbbyyyz 等 nono5.107.0YES5.2.2YESYESnonononononoECMA
1.42–1.83
nononononononono
命名子常式呼叫 (?P>name),其中「name」為擷取群組的名稱 擷取群組的遞迴或子常式呼叫至擷取群組。 a(?P<x>b(?P>x)?y)z 符合 abyz、abbyyz、abbbyyyz 等 nono5.10YESYESYESYESYESnonononononoECMA
1.42–1.83
nononononononono
命名子常式呼叫 \g<name>,其中「name」為擷取群組的名稱 擷取群組的遞迴或子常式呼叫至擷取群組。 a(?<x>b\g<x>?y)z 符合 abyz、abbyyz、abbbyyyz 等 nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
命名子常式呼叫 \g'name',其中「name」為擷取群組的名稱 擷取群組的遞迴或子常式呼叫至擷取群組。 a(?'x'b\g'x'?y)z 符合 abyz、abbyyz、abbbyyyz 等 nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
子常式定義 (?(DEFINE)regex),其中「regex」為任意正規表示式 DEFINE 群組不參與比對程序。可以在 DEFINE 群組內對擷取群組進行子常式呼叫。 (?(DEFINE)([ab]))
x(?1)y(?1)z
符合 xayaz、xaybz、xbyaz 和 xbybz
nono5.107.0YES5.2.2YESYESnonononononoECMA
1.42–1.83
nononononononono
子常式呼叫擷取 使用 Ruby 風格 \g 語法進行子常式呼叫 對擷取群組的子常式呼叫會讓該擷取群組儲存子常式呼叫期間符合的文字。 當 ([ab])\g'1' 符合 ab 時,第一個擷取群組會在符合後儲存 b。 不適用不適用不適用nonononono不適用不適用不適用不適用1.9不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用
子常式呼叫擷取 使用 \g 以外的語法進行子常式呼叫 對擷取群組的子常式呼叫會讓該擷取群組儲存子常式呼叫期間符合的文字。 當 ([ab])(?1) 符合 ab 時,第一個擷取群組會在符合後儲存 b。 不適用nonononononono不適用不適用不適用不適用不適用不適用no不適用不適用不適用不適用不適用不適用不適用不適用
遞迴隔離擷取群組 任何遞迴或子常式呼叫 每個子常式呼叫都有自己的擷取群組獨立儲存空間。呼叫中的反向參照無法看到呼叫之前的符合文字,呼叫之後的反向參照也無法看到呼叫中的符合文字。遞迴中的反向參照無法看到其他遞迴層級的符合文字。 (a)(\1)(?2) 永遠不會符合任何東西,因為 \1 總會在 (?2) 進行的呼叫中失敗。 不適用no5.10–5.18nonononono不適用不適用不適用不適用no不適用no不適用不適用不適用不適用不適用不適用不適用不適用
遞迴還原擷取群組 使用 Ruby 風格 \g 語法的遞迴或子常式呼叫 當正規表示式引擎退出遞迴或子常式呼叫時,它會將所有擷取群組還原為進入遞迴或子常式呼叫之前符合的文字。 當 (a)(([bc])\1)\g'2' 符合 abaca 時,第三個群組會在符合後儲存 b 不適用不適用不適用7.7YES5.2.7YESYES不適用不適用不適用不適用no不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用
遞迴還原擷取群組 使用 \g 以外的語法進行遞迴或子常式呼叫 當正規表示式引擎退出遞迴或子常式呼叫時,它會將所有擷取群組還原為進入遞迴或子常式呼叫之前符合的文字。 當 (a)(([bc])\1)(?2) 比對 abaca 時,第三個群組會在比對後儲存 b 不適用no5.10YESYESYESYESYES不適用不適用不適用不適用不適用不適用ECMA
1.42–1.83
不適用不適用不適用不適用不適用不適用不適用不適用
遞迴不會孤立或還原擷取群組 使用 Ruby 風格 \g 語法的遞迴或子常式呼叫 遞迴和子常式呼叫不會對擷取群組給予任何特殊處理,除了子常式呼叫會擷取之外。反向參照總是會看到每個擷取群組最近比對到的文字,無論它們是否在同層遞迴中。 當 (a)(([bc])\1)\g'2' 比對 abaca 時,第三個群組會在比對後儲存 c 不適用不適用不適用nonononono不適用不適用不適用不適用1.9不適用不適用不適用不適用不適用不適用不適用不適用不適用不適用
遞迴不會孤立或還原擷取群組 使用 \g 以外的語法進行遞迴或子常式呼叫 遞迴和子常式呼叫不會對擷取群組給予任何特殊處理,除了子常式呼叫會擷取之外。反向參照總是會看到每個擷取群組最近比對到的文字,無論它們是否在同層遞迴中。 當 (a)(([bc])\1)(?2) 比對 abaca 時,第三個群組會在比對後儲存 c 不適用僅 4nononononono不適用不適用不適用不適用不適用不適用no不適用不適用不適用不適用不適用不適用不適用不適用
遞迴是原子的 使用 (?P>…) 的遞迴或子常式呼叫 遞迴和子常式呼叫是原子的。一旦正規表示式引擎退出它們,它就不會回溯到它們中嘗試遞迴或子常式呼叫的不同排列。 (a+)(?P>1)(?P>1) 永遠無法比對任何東西,因為第一個 (?P>1) 比對所有剩下的 a,而當第二個失敗時,正規表示式引擎不會回溯到第一個 (?P>1) 不適用不適用no6.510.00–10.235.1.3–7.2.34YES2.14.0–3.6.3不適用不適用不適用不適用不適用不適用no不適用不適用不適用不適用不適用不適用不適用不適用
遞迴是原子的 使用 (?P>0) 以外的語法遞迴整個正規表示式 整個正規表示式的遞迴是原子的。一旦正規表示式引擎退出遞迴,它就不會回溯到它中嘗試遞迴的不同排列。 aa$|a(?R)a|a 在遞迴為原子的情況下,會在 aaa 中比對 a;否則它會比對整個字串。 不適用不適用no6.5YES5.1.3–7.2.34YESYES不適用不適用不適用不適用no不適用ECMA
1.42–1.83
不適用不適用不適用不適用不適用不適用不適用不適用
遞迴是原子的 使用語法呼叫子常式,而非 (?P>…) 子常式呼叫是原子性的。一旦正規表示式引擎從中退出,它就不會回溯到其中嘗試子常式呼叫的不同排列。 (a+)(?1)(?1) 永遠無法配對任何東西,因為第一個 (?1) 配對所有剩下的 a,而當第二個失敗時,正規表示式引擎不會回溯到第一個 (?1) 不適用僅 4no6.510.00–10.235.1.3–7.2.34YES2.14.0–3.6.3不適用不適用不適用不適用no不適用no不適用不適用不適用不適用不適用不適用不適用不適用
功能語法說明範例.NET Java Perl PCRE PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE POSIX BRE POSIX ERE GNU BRE GNU ERE Oracle XML XPath
Regex Reference: Balancing Groups, Recursion, and Subroutines
  • 简
  • 繁
  • En
About Regular Expressions » Regular Expressions Reference » Regex Reference: Balancing Groups, Recursion, and Subroutines

Regex Reference
Introduction
Table of Contents
Quick Reference
Characters
Basic Features
Character Classes
Shorthands
Anchors
Word Boundaries
Quantifiers
Unicode
Capturing Groups & Backreferences
Named Groups & Backreferences
Special Groups
Mode Modifiers
Recursion & Balancing Groups
Replacement Reference
Characters
Matched Text & Backreferences
Context & Case Conversion
Conditionals
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

Regex Reference: Balancing Groups, Recursion, and Subroutines

FeatureSyntaxDescriptionExample.NET Java Perl PCRE PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE POSIX BRE POSIX ERE GNU BRE GNU ERE Oracle XML XPath
Balancing group (?<capture-subtract>regex) where “capture” and “subtract” are group names and “regex” is any regex The name “subtract” must be used as the name of a capturing group elsewhere in the regex. If this group has captured matches that haven’t been subtracted yet, then the balancing group subtracts one capture from “subtract”, attempts to match “regex”, and stores its match into the group “capture”. If “capture” is omitted, the same happens without storing the match. If “regex” is omitted, the balancing group succeeds without advancing through the string. If the group “subtract” has no matches to subtract, then the balancing group fails to match, regardless of whether “regex” is specified or not. ^(?<l>\w)+\w?
(\k<l>(?<-l>))+
(?(l)(?!))$
matches any palindrome word
YESnononononononononononononononononononononono
Balancing group (?'capture-subtract'regex) where “capture” and “subtract” are group names and “regex” is any regex The name “subtract” must be used as the name of a capturing group elsewhere in the regex. If this group has captured matches that haven’t been subtracted yet, then the balancing group subtracts one capture from “subtract”, attempts to match “regex”, and stores its match into the group “capture”. If “capture” is omitted, the same happens without storing the match. If “regex” is omitted, the balancing group succeeds without advancing through the string. If the group “subtract” has no matches to subtract, then the balancing group fails to match, regardless of whether “regex” is specified or not. ^(?'l'\w)+\w?
(\k'l'(?'-l'))+
(?(l)(?!))$
matches any palindrome word
YESnononononononononononononononononononononono
Recursion (?R) Recursion of the entire regular expression. a(?R)?z matches az, aazz, aaazzz, etc. nono5.10YESYESYESYESYESnonononononoECMA
1.42–1.83
nononononononono
Recursion (?0) Recursion of the entire regular expression. a(?0)?z matches az, aazz, aaazzz, etc. nono5.10YESYESYESYESYESnonononononoECMA
1.42–1.83
nononononononono
Recursion \g<0> Recursion of the entire regular expression. a\g<0>?z matches az, aazz, aaazzz, etc. nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
Recursion \g'0' Recursion of the entire regular expression. a\g'0'?z matches az, aazz, aaazzz, etc. nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
Subroutine call (?1) where 1 is the number of a capturing group Recursion of a capturing group or subroutine call to a capturing group. a(b(?1)?y)z matches abyz, abbyyz, abbbyyyz, etc. no4 only5.10YESYESYESYESYESnonononononoECMA
1.42–1.83
nononononononono
Subroutine call \g<1> where 1 is the number of a capturing group Recursion of a capturing group or subroutine call to a capturing group. a(b\g<1>?y)z matches abyz, abbyyz, abbbyyyz, etc. nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
Subroutine call \g'1' where 1 is the number of a capturing group Recursion of a capturing group or subroutine call to a capturing group. a(b\g'1'?y)z matches abyz, abbyyz, abbbyyyz, etc. nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
Relative subroutine call (?-1) where -1 is a negative integer Recursion of or subroutine call to a capturing group that can be found by counting as many opening parentheses of named or numbered capturing groups as specified by the number from right to left starting at the subroutine call. a(b(?-1)?y)z matches abyz, abbyyz, abbbyyyz, etc. nono5.107.2YES5.2.4YESYESnonononononoECMA
1.42–1.83
nononononononono
Relative subroutine call \g<-1> where -1 is a negative integer Recursion of or subroutine call to a capturing group that can be found by counting as many opening parentheses of named or numbered capturing groups as specified by the number from right to left starting at the subroutine call. a(b\g<-1>?y)z matches abyz, abbyyz, abbbyyyz, etc. nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
Relative subroutine call \g'-1' where -1 is a negative integer Recursion of or subroutine call to a capturing group that can be found by counting as many opening parentheses of named or numbered capturing groups as specified by the number from right to left starting at the subroutine call. a(b\g'-1'?y)z matches abyz, abbyyz, abbbyyyz, etc. nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
Forward subroutine call (?+1) where +1 is a positive integer Recursion of or subroutine call to a capturing group that can be found by counting as many opening parentheses of named or numbered capturing groups as specified by the number from left to right starting at the subroutine call. (?+1)x([ab]) matches axa, axb, bxa, and bxb nono5.107.2YES5.2.4YESYESnonononononoECMA
1.42–1.83
nononononononono
Forward subroutine call \g<+1> where +1 is a positive integer Recursion of or subroutine call to a capturing group that can be found by counting as many opening parentheses of named or numbered capturing groups as specified by the number from left to right starting at the subroutine call. \g<+1>x([ab]) matches axa, axb, bxa, and bxb nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
Forward subroutine call \g'+1' where +1 is a positive integer Recursion of or subroutine call to a capturing group that can be found by counting as many opening parentheses of named or numbered capturing groups as specified by the number from left to right starting at the subroutine call. \g'+1'x([ab]) matches axa, axb, bxa, and bxb nonono7.7YES5.2.7YESYESnononono2.0nononononononononono
Named subroutine call (?&name) where “name” is the name of a capturing group Recursion of a capturing group or subroutine call to a capturing group. a(?<x>b(?&x)?y)z matches abyz, abbyyz, abbbyyyz, etc. nono5.107.0YES5.2.2YESYESnonononononoECMA
1.42–1.83
nononononononono
Named subroutine call (?P>name) where “name” is the name of a capturing group Recursion of a capturing group or subroutine call to a capturing group. a(?P<x>b(?P>x)?y)z matches abyz, abbyyz, abbbyyyz, etc. nono5.10YESYESYESYESYESnonononononoECMA
1.42–1.83
nononononononono
Named subroutine call \g<name> where “name” is the name of a capturing group Recursion of a capturing group or subroutine call to a capturing group. a(?<x>b\g<x>?y)z matches abyz, abbyyz, abbbyyyz, etc. nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
Named subroutine call \g'name' where “name” is the name of a capturing group Recursion of a capturing group or subroutine call to a capturing group. a(?'x'b\g'x'?y)z matches abyz, abbyyz, abbbyyyz, etc. nonono7.7YES5.2.7YESYESnononono1.9nononononononononono
Subroutine definitions (?(DEFINE)regex) where “regex” is any regex The DEFINE group does not take part in the matching process. Subroutine calls can be made to capturing groups inside the DEFINE group. (?(DEFINE)([ab]))
x(?1)y(?1)z
matches xayaz, xaybz, xbyaz, and xbybz
nono5.107.0YES5.2.2YESYESnonononononoECMA
1.42–1.83
nononononononono
Subroutine calls capture Subroutine call using Ruby-style \g syntax A subroutine call to a capturing group makes that capturing group store the text matched during the subroutine call. When ([ab])\g'1' matches ab the first capturing group holds b after the match. n/an/an/anononononon/an/an/an/a1.9n/an/an/an/an/an/an/an/an/an/a
Subroutine calls capture Subroutine call using syntax other than \g A subroutine call to a capturing group makes that capturing group store the text matched during the subroutine call. When ([ab])(?1) matches ab the first capturing group holds b after the match. n/anononononononon/an/an/an/an/an/anon/an/an/an/an/an/an/an/a
Recursion isolates capturing groups Any recursion or subroutine call Each subroutine call has its own separate storage space for capturing groups. Backreferences inside the call cannot see text matched before the call, and backreferences after the call cannot see text matched inside the call. Backreferences inside recursion cannot see text matched at other recursion levels. (a)(\1)(?2) never matches anything because \1 always fails during the call made by (?2). n/ano5.10–5.18nononononon/an/an/an/anon/anon/an/an/an/an/an/an/an/a
Recursion reverts capturing groups Recursion or subroutine call using Ruby-style \g syntax When the regex engine exits from recursion or a subroutine call, it reverts all capturing groups to the text they had matched prior to entering the recursion or subroutine call. When (a)(([bc])\1)\g'2' matches abaca the third group stores b after the match n/an/an/a7.7YES5.2.7YESYESn/an/an/an/anon/an/an/an/an/an/an/an/an/an/a
Recursion reverts capturing groups Recursion or subroutine call using syntax other than \g When the regex engine exits from recursion or a subroutine call, it reverts all capturing groups to the text they had matched prior to entering the recursion or subroutine call. When (a)(([bc])\1)(?2) matches abaca the third group stores b after the match n/ano5.10YESYESYESYESYESn/an/an/an/an/an/aECMA
1.42–1.83
n/an/an/an/an/an/an/an/a
Recursion does not isolate or revert capturing groups Recursion or subroutine call using Ruby-style \g syntax Capturing groups are not given any special treatment by recursion and subroutine calls, except perhaps that subroutine calls capture. Backreferences always see the text most recently matched by each capturing group, regardless of whether they are inside the same level of recursion or not. When (a)(([bc])\1)\g'2' matches abaca the third group stores c after the match n/an/an/anononononon/an/an/an/a1.9n/an/an/an/an/an/an/an/an/an/a
Recursion does not isolate or revert capturing groups Recursion or subroutine call using syntax other than \g Capturing groups are not given any special treatment by recursion and subroutine calls, except perhaps that subroutine calls capture. Backreferences always see the text most recently matched by each capturing group, regardless of whether they are inside the same level of recursion or not. When (a)(([bc])\1)(?2) matches abaca the third group stores c after the match n/a4 onlynonononononon/an/an/an/an/an/anon/an/an/an/an/an/an/an/a
Recursion is atomic Recursion or subroutine call using (?P>…) Recursion and subroutine calls are atomic. Once the regex engine exits from them, it will not backtrack into it to try different permutations of the recursion or subroutine call. (a+)(?P>1)(?P>1) can never match anything because the first (?P>1) matches all remaining a’s and the regex engine won’t backtrack into the first (?P>1) when the second one fails n/an/ano6.510.00–10.235.1.3–7.2.34YES2.14.0–3.6.3n/an/an/an/an/an/anon/an/an/an/an/an/an/an/a
Recursion is atomic Recursion of the whole regex using syntax other than (?P>0) Recursion of the whole regex is atomic. Once the regex engine exits from recursion, it will not backtrack into it to try different permutations of the recursion. aa$|a(?R)a|a matches a in aaa when recursion is atomic; otherwise it would match the whole string. n/an/ano6.5YES5.1.3–7.2.34YESYESn/an/an/an/anon/aECMA
1.42–1.83
n/an/an/an/an/an/an/an/a
Recursion is atomic Subroutine call using syntax other than (?P>…) Subroutine calls are atomic. Once the regex engine exits from them, it will not backtrack into it to try different permutations of the subroutine call. (a+)(?1)(?1) can never match anything because the first (?1) matches all remaining a’s and the regex engine won’t backtrack into the first (?1) when the second one fails n/a4 onlyno6.510.00–10.235.1.3–7.2.34YES2.14.0–3.6.3n/an/an/an/anon/anon/an/an/an/an/an/an/an/a
FeatureSyntaxDescriptionExample.NET Java Perl PCRE PCRE2 PHP Delphi R JavaScript VBScript XRegExp Python Ruby std::regex Boost Tcl ARE POSIX BRE POSIX ERE GNU BRE GNU ERE Oracle XML XPath
©2015-2025 艾丽卡 support@alaica.com