更新版本的Perl上的正则表达式匹配问题

And*_*wby 3 regex perl

我已经使用Perl 5.22.1转移到新服务器.我有这段代码:

$html =~ m{
    ( # $1 the whole tag
        <
        (
            ?:
            !--
            ( # $2 the attributes are all the data between
                .*?
            )
            --
            | # or
            (
                ?:
                ( # $3 the name of the tag
                    /?\S+?\b
                )
                ( # $4 the attributes
                    [^'">]*
                    (
                        ?:
                        ( # $5 just to match quotes
                            ['"]
                        )
                        .*?\5
                        [^'">]*
                    )*
                )
            )
        )
        >
    )
}gsx
Run Code Online (Sandbox Code Playgroud)

...现在它给了我这个错误:

A fatal error has occurred:

    In '(?...)', the '(' and '?' must be adjacent in regex; marked by <-- HERE in m/
                ( # $1 the whole tag
                    <
                    (
                        ? <-- HERE :
                        !--
                        ( # $2 the attributes are all the data between
                            .*?
                        )
                        --
                        | # or
                        (
                            ?:
                            ( # $3 the name of the tag
                                /?\S+?\b
                            )
                            ( # $4 the attributes
                                [^'">]*
                                (
                                    ?:
                                    ( # $5 just to match quotes
                                        ['"]
                                    )
                                    .*?\5
                                    [^'">]*
                                )*
                            )
                        )
                    )
                    >
                )
            / at ./admin/GT/HTML/Parser.pm line 207.
    Compilation failed in require at (eval 25) line 8.

Please enable debugging in setup for more details.
Run Code Online (Sandbox Code Playgroud)

我不太确定它在抱怨什么.有任何想法吗?

Wik*_*żew 5

您需要确保?:(非捕获组标记)在开括号之后, 即使使用x修饰符也是如此.

请参阅固定的正则表达式声明:

$html =~ m{
    ( # $1 the whole tag
        <
        (?:
            !--
            ( # $2 the attributes are all the data between
                .*?
            )
            --
            | # or
            (?:
                ( # $3 the name of the tag
                    /?\S+?\b
                )
                ( # $4 the attributes
                    [^'">]*
                    (?:
                        ( # $5 just to match quotes
                            ['"]
                        )
                        .*?\5
                        [^'">]*
                    )*
                )
            )
        )
        >
    )
}gsx
Run Code Online (Sandbox Code Playgroud)

看到这个参考:

请注意,任何内部的任何内容都\Q...\E不受影响/x.请注意,/x这不会影响单个多字符构造中的空间解释.例如\x{...},无论/x修饰符如何,都不能有空格.对于诸如{3}或等的量词也是如此{5,}.同样,(?:...)不能有之间的空间"{","?"":".在这种构造的任何分隔符内,允许的空间不受构造的影响/x,并且取决于构造.例如,\x{...}不能有空格,因为十六进制数字中没有空格.

我认为有一个错字 - {必须是实际的(.我加粗了与当前场景相关的部分文本.