2014-01-07 116 views
0

我的正则表达式是这样的:Perl的正则表达式的解释

s/<(?:[^>'"]|(['"]).?\1)*>//gs 

,我不知道究竟是什么意思。

+0

如果您将正则表达式输入到http://gskinner.com/RegExr/并将鼠标悬停在每一块上,它将在工具提示中解释它。 – Barmar

+2

哪部分尤其你不明白?它有助于知道要解释什么 – ysth

+0

我认为该行的总体意图是从输入中删除所有HTML标记。 – Barmar

回答

1

正则表达式旨在从输入中删除HTML标记。

它匹配以<开头并以>结尾的文本,其中包含非> /非引号或引用字符串(可能包含>)。但它似乎有一个错误:

.?说,报价可能包含0或1个字符;它可能打算是.*?(0个或更多字符)。并且为了防止回溯在某些奇怪的情况下使.匹配报价,它需要将(?: ...)分组更改为占有(>而不是:)。

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 
-------------------------------------------------------------------------------- 
    >      '>' 

因此试图删除HTML标签作为YSTH也提到了。

+0

URL不起作用 – rbm

+0

看来服务已经坏了,无论如何,结果都在答案中。 –