用 Regex 擷取中括弧&取代空白

rusli
Site Admin
文章: 212
註冊時間: 週三 7月 07, 2010 9:49 pm

用 Regex 擷取中括弧&取代空白

文章rusli » 週四 9月 06, 2012 2:33 pm

用 Regex 擷取中括弧&取代空白
Regex.Escape 太酷了!

[syntax="csharp"]
string input = "[>999999999] 0917307543 [>999999999] [>999999999]";
string pattern = Regex.Escape("[") + "(.*?)]";
MatchCollection matches = Regex.Matches(input, pattern);
string result = input;

foreach (Match match in matches)
{
result = result.Replace(match.Value, string.Empty);
}
[/syntax]

回到「C#」

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客