2014-01-07 119 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

看來服務已經壞了,無論如何,結果都在答案中。 –