2014-02-06 24 views
0

我想問一下正則表達式問題。我想在vs 2008中搜索以下語法:html標籤attr在vs 2008中的正則表達式

<table width="10%" class="test" style=""> </table> 
<table class="test" style="" width="10%"> </table> 
<table class="test" width="10%" style=""> </table> 
<table class="test" width="10%"></table> 
<table class="test"></table> 
<table></table> 
<div width="10%"></div> 

我想在上面的表格文本中搜索所有width =。

搜索正則表達式

table[^\w]+width="[^"]+" 

搜索結果

<table width="10%" class="test" style=""> </table> 
<table class="test" style="" width="10%"> </table> 
<table class="test" width="10%" style=""> </table> 
<table class="test" width="10%"></table> 

後,我想更換寬度風格= 「WIDTH:」

結果應該是

<table style="width:10%" class="test" style=""> </table> 
<table class="test" style="" style="width:10%"> </table> 
<table class="test" style="width:10%" style=""> </table> 
<table class="test" style="width:10%"></table> 

我如何做這個替換?

+2

通常,[正則表達式不是解析HTML的正確工具](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)。 – Vulcan

回答

0

使用以下正則表達式:

width="[^"]+" 

[^"]匹配,是不是"任何字符。

+0

編輯問題 – user3277955

+0

@ user3277955,你想刪除沒有'width'屬性的標籤嗎?請說清楚。 – falsetru

+0

我想將寬度格式替換爲style =「width:」格式 – user3277955