System.Text.RegularExpressions.Regex

關鍵字 描述 範例 用途
System.Text.RegularExpressions.Regex 提供正則運算式功能,用於文字處理,包括匹配、替換和分割字串。 Regex regex = new Regex(@"\\\\d+"); 用來處理和分析字串中的模式。
IsMatch() 判斷字串是否符合正則運算式的模式。 bool isMatch = regex.IsMatch("123"); 檢查字串是否符合指定的正則運算式。
Match() 在字串中找到符合正則運算式模式的第一個匹配項。 Match match = regex.Match("123abc"); 獲取符合模式的第一個匹配結果。
Matches() 在字串中找到所有符合正則運算式模式的匹配項。 MatchCollection matches = regex.Matches("123abc 456def"); 獲取所有符合模式的匹配結果。
Replace() 使用指定的字串替換所有符合正則運算式模式的部分。 string result = regex.Replace("123abc", "X"); 替換字串中所有符合模式的部分。
Split() 根據正則運算式模式分割字串。 string[] result = regex.Split("123abc 456def"); 使用正則運算式模式分割字串。