1

I'm matching on LaTeX-Commands of the form \command{...}{...}. The second argument is optional. My RegEx is a only slightly modificated version of one example in perl6 faq because I need to take care of the case that there may be nested LaTeX commands inside the arguments.

I want to use named groups. How can I do this? I tried to use (?<first>:...) together with (?&first), but it gives me an "infinite recursion" error. I might be a little over my head in terms of RegExes here, but this worked very nicely so far.

my $regex = qr/
          \\command
            (\{
              (?:
                [^\{\}]++
                  |
                (?1)
              )*
            \})
           (\{
              (?:
                [^\{\}]++
                  |
                (?2)
              )*
           \})?
        /x;

$s =~ m/$regex/g
4

1 回答 1

0

您应该能够使用命名组,例如s/(?<first>foo+)/\k<first>bar/ors/(?first:foo+)/\g{first}bar/(?<first>:...)将其:视为模式的一部分。

我不确定为什么你的(?PARNO)表达式会导致无限递归,但是当做同样的事情时,手册有一个额外的括号组。

于 2011-11-25T04:31:02.597 回答