Ruby正则表达式中递归嵌套匹配的花括号对

Dom*_*Dom 6 ruby regex

我有以下字符串:

The {quick} brown fox {jumps {over {deep} the} {sfsdf0} lazy} dog {sdfsdf1 {sdfsdf2}
Run Code Online (Sandbox Code Playgroud)

和PHP正则表达式:

/(?=\{((?:[^{}]+|\{(?1)\})+)\})/g
Run Code Online (Sandbox Code Playgroud)

它产生以下匹配:

[5-10]  `quick`
[23-60] `jumps {over {deep} the} {sfsdf} lazy`
[30-45] `over {deep} the`
[36-40] `deep`
[48-54] `sfsdf0`
[76-83] `sdfsdf2`
Run Code Online (Sandbox Code Playgroud)

请参阅:http://regex101.com/r/fD3iZ2.

我正在尝试使用Ruby中的等效工具,但我遇到了问题(?1)...导致undefined group option错误:

str = "The {quick} brown fox {jumps {over {deep} the} {sfsdf} lazy} dog {sdfsdf {sdfsdf}"
str.scan /(?=\{((?:[^{}]+|\{(?1)\})+)\})/

SyntaxError: undefined group option: /(?=\{((?:[^{}]+|\{(?1)\})+)\})/
Run Code Online (Sandbox Code Playgroud)

见:http://fiddle.re/n6w4n.

巧合的是,我在Javascript和Python中遇到了同样的错误.

今天我的正则表达式几乎已经筋疲力尽了,非常感谢任何帮助.

Tim*_*ker 15

Ruby使用不同的语法进行递归:\g<1>替换(?1).所以试试吧

(?=\{((?:[^{}]++|\{\g<1>\})++)\})
Run Code Online (Sandbox Code Playgroud)

我还使量词具有占有性,以避免在不平衡支撑的情况下过度回溯.

irb(main):003:0> result = str.scan(/(?=\{((?:[^{}]++|\{\g<1>\})++)\})/)
=> [["quick"], ["jumps {over {deep} the} {sfsdf} lazy"], ["over {deep} the"], 
["deep"], ["sfsdf"], ["sdfsdf"]]
Run Code Online (Sandbox Code Playgroud)