2013-05-07 20 views
-3

我有一些字符串這樣的:如何找到內支架上的字,串並用新的數據替換

這是某種[串,10],我想用[anotherstring,10來代替它]

我想找到[串,10][anotherstring,10],並用新的數據替換它。

我該怎麼做?

+0

括號內的任何單詞或只是一個單詞之後,10 – Steve 2013-05-07 15:11:47

+0

10,重要嗎? – 2013-05-07 15:12:05

+0

任何字詞。我必須從數據庫中獲取一些數據取決於括號內的那個單詞。 – Raika 2013-05-07 15:12:29

回答

2

下面是一個例子regex replace,使用MatchEvaluator做一些事情,結果複雜:

Regex.Replace(
"this is some [string,10] and i want to replace it with [anotherstring,10]", 
@"\[(.*?),(\d+)\]", 
m => "[was " + m.Groups[1].ToString() + "::" + int.Parse(m.Groups[2].ToString()) + "]") 

它單獨捕獲字符串和數字,所以你可以單獨做的事情與他們。如果您只需要它是,10,則可以執行@"\[(.*?),10\]"模式。或者,如果你想要的任何角色裏面作爲一個值對待,@"\[(.*?)\]"如圖所示,它導致:

這是一些[是字符串:: 10],我想用[是 anotherstring來代替它:: 10]

+0

謝謝你的工作。有什麼辦法可以得到我的括號列表嗎? – Raika 2013-05-07 15:21:49

+1

'Regex.Matches(「這是一些[字符串,10],我想用[anotherstring,10]替換它,」@「\ [(。*?),(\ d +)\]」)' – 2013-05-07 15:22:30

+0

謝謝人。這是幫助 – Raika 2013-05-07 15:25:23

相關問題