2010-07-07 46 views

回答

3

這裏是什麼,我認爲你的例子所要求的:

Match m = Regex.Match("something some stuff in the middle someotherthing", 
     "something (.*) someotherthing"); 
if (m.Success) 
    Console.WriteLine("groups={0}, entire match={1}, first group={2}", 
         m.Groups.Count, m.Groups[0].Value, 
         m.Groups[1].Value); 
2

哪些地方可以使用$1獲得該組中,您可以使用$0讓整個表達式。

這適用於大多數正則表達式變體(可能是\0%0或其他),而不僅僅是C#。

同樣,Match.Groups property應該以0作爲參數,返回整個匹配,否則Capture.Value看起來像包含匹配。


另外值得一提的,以確保您的測試字符串的整個則表達式匹配,它通常是前面加上^和後綴與$這對於開始和字符串的結尾零寬度位置錨是個好主意。 (也啓動/在多行模式行尾。)

相關問題