2011-03-27 26 views
0

我需要在我的文檔中用另一個字符串替換<jdoc:include type="component" />。因此,空白是對我很重要,因爲表達可能是像所有的這些或更多:正則表達式要找到<jdoc:include type =「component」/>

<jdoc:include type="component"   /> 
<jdoc:include     type="component" /> 
<jdoc:include type="component" xxxxx /> 

或更多...

+0

用什麼語言,在什麼平臺上? – 2011-03-27 09:05:19

+0

你想在哪裏取代哪一部分? – karlphillip 2011-03-27 15:52:15

回答

0

雖然與玩的bash sed的,我做如下:創建一個名爲test.php的文件,其中包含所有的字符串:

<jdoc:include type="component"   /> 
<jdoc:include     type="component" /> 
<jdoc:include type="component" xxxxx /> 

然後闡述了一個匹配所有情況的單個正則表達式,並將它們替換爲<jdoc:include type="stackoverflow" />

享受:

sed -r 's/<jdoc:include\s+type=\"component\"\s+\w*\s*\/>/<jdoc:include type=\"stackoverflow\" \/>/' test.php 

如果正在徘徊:

\s+意味着匹配1個或多個空格

\w*裝置在範圍0匹配任何字符 - 9,A - Z和一個 - z

This page是一個很好的正則表達式資源。

+0

@ShirazITCo對於我們所有人來說,查看答案並接受解決問題的答案非常重要。 – karlphillip 2011-03-29 18:34:58