2010-03-19 67 views
1

我有一些文本中包含HTML超鏈接。 我想刪除超鏈接,但只有特定的鏈接。正則表達式 - 根據鏈接文本移除HTML超鏈接

例如我開始與此:

This is text <a href="link/to/somewhere">Link to Remove</a> and more text with another link <a href="/link/to/somewhere/else">Keep this link</a> 

我想有:

This is text and more text with another link <a href="/link/to/somewhere/else">Keep this link</a> 

我有這樣的正則表達式表達,

<a\s[^>]*>.*?</a> 

...但它匹配所有的鏈接。

我需要添加到該表達式才能匹配鏈接文本中的「刪除」(例如)鏈接?

在此先感謝。

+0

http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/ 1732454#1732454 – thecoop 2010-03-22 15:24:59

回答

1

你可能會得到很多反饋不要在使用正則表達式的HTML ...但如果你決定使用一個,試試這個:

<a\s[^>]*>.*?Remove.*?</a> 

這是「刪除」介於在鏈接文本中。

+0

謝謝,明白了。 如果我想在不區分大小寫的情況下匹配'刪除',我會將它包裹在什麼? (例如匹配'刪除'或'刪除'或'刪除'等等......) – Rob 2010-03-19 04:59:02

+2

@Rob:很確定C#有類似'RegexOptions.IgnoreCase'的東西,你可以作爲另一個參數傳入。 – mpen 2010-03-19 05:19:31

0
$str=~/(.*)<a.*<\/a>([a-z ]+ <a.*<\/a>)/; 
print "$1$2"; 
0

(.?)< a。 * [Rr] emove。 *一個>(*。)

重建有:$ 1 $ 2