1 頁 (共 1 頁)

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

發表於 : 週四 9月 06, 2012 2:33 pm
rusli
用 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]