gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:20:25 25 4
gpt4 key购买 nike

我已经转移到一个新的服务器,使用 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

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

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.

我不太确定它在提示什么。有什么想法吗?

最佳答案

您需要确保 ?:(非捕获组标记)紧跟在左括号之后 即使 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

参见 this reference :

Note that anything inside a \Q...\E stays unaffected by /x. And note that /x doesn't affect space interpretation within a single multi-character construct. For example in \x{...}, regardless of the /x modifier, there can be no spaces. Same for a quantifier such as {3} or {5,}. Similarly, (?:...) can't have a space between the "{" , "?" , and ":". Within any delimiters for such a construct, allowed spaces are not affected by /x, and depend on the construct. For example, \x{...} can't have spaces because hexadecimal numbers don't have spaces in them.

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

关于较新版本的 Perl 上的正则表达式匹配问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44516577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com