2012-11-14 41 views
0

如何在不匹配給定文本前綴的情況下替換文本值?正則表達式如果單詞與給定前綴不匹配,則替換髮現的所有匹配項

例如:

test hello world... I know hello world, this seems hello world..

那麼我們的替代值是 「HI」

文會..

test hello world... I know HI, this seems HI..

+0

這個詞的時候「測試」是不是之前的「Hello World」你是什麼意思? –

+0

所以你的意思是你想要用hi代替字符串中的hello世界 –

+0

如果在找到hello世界之前進行了單詞測試,將不會有替換過程。但如果沒有找到,你好世界將被替換爲HI – lexeRoy

回答

1
(?<!test\s)\bhello world\b 

本作當你直接進行測試時,你會對測試感興趣。

+0

謝謝。這不知何故幫助我:) – lexeRoy

0

是有點聰明,避免正則表達式,你可能會做這樣的事情

string v = s.Replace("hello world", "HI"); //replace hello world in all occurrences with HI 

string newstring = v.Replace("test HI", "hello world"); //place hello world where hi is after test