今天在利用 Regular Expression 取得網頁中的某些資料時,發現因為 pattern 都是包在 ( ) 內,如果Html 的內容也有 ( ) 符號,好像會比對不到資料
查了一下,可以用 Regex.Escape("(") 來將符號轉成字串查詢
這樣就可以取到 (02) 了
string pattern = Regex.Escape("(") + "(.*?)" + Regex.Escape(")");
string input = "(02)1234567";
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
MessageBox.Show(match.Value);
}
沒有留言:
張貼留言