Recursive nested pairs of curly bracket matches in Ruby regex

I have the following line:

The {quick} brown fox {jumps {over {deep} the} {sfsdf0} lazy} dog {sdfsdf1 {sdfsdf2} 

And the PHP regular expression:

 /(?=\{((?:[^{}]+|\{(?1)\})+)\})/g 

It gives the following matches:

 [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` 

See: http://regex101.com/r/fD3iZ2 .

I am trying to get an equivalent working in Ruby, but I have a problem with (?1) ... the result is an undefined group option error:

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

See: http://fiddle.re/n6w4n .

Coincidentally, I get the same error in Javascript and Python.

My regex foo is almost exhausted today, any help is much appreciated.

+6
source share
1 answer

Ruby uses a different syntax for recursion: \g<1> replaces (?1) . Therefore try

 (?=\{((?:[^{}]++|\{\g<1>\})++)\}) 

I also made the quantifiers the owners to avoid excessive backtracking in the event of unbalanced braces.

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

Source: https://habr.com/ru/post/956380/


All Articles