[英]Perl regular expression explanation
我有这样的正则表达式:
s/<(?:[^>'"]|(['"]).?\1)*>//gs
我不知道这到底是什么意思。
正则表达式看起来旨在从输入中删除HTML标记。
它匹配文本开头<
并与结尾>
,含有非>
/非引号或引用的字符串(其可以包含>
)。 但它似乎有一个错误:
.?
说引号可以包含0或1个字符; 它可能是.*?
(0个或更多字符)。 并防止回溯做类似的事情.
在某些情况下,奇匹配报价,它需要改变(?: ... )
分组是占有欲( >
而不是:
)。
此工具可以解释详细信息: http : //rick.measham.id.au/paste/explain.pl?regex=%3C%28%3F% 3A[^% 3E%27%22]|%28[%27 %22]%29%3F \\ 1%29 *%3E
NODE EXPLANATION
--------------------------------------------------------------------------------
< '<'
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
[^>'"] any character except: '>', ''', '"'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
['"] any character of: ''', '"'
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
.? any character except \n (optional
(matching the most amount possible))
--------------------------------------------------------------------------------
\1 what was matched by capture \1
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
> '>'
因此,它尝试删除ysth也提到的HTML标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.