protected void Page_Load(object sender, EventArgs e)
{
//要搜尋的字串
string text = "The the 《quick brown fox》fox 《jumped over the lazy dog》 dog.";
Response.Write(GetValue(text, "《", "》"));
}
public static string GetValue(string text, string start, string end)
{
string resule = "要搜尋的字串:" + text + "<br>用正則表達式獲取中間的值 ==> ";
//定義正則表達式,設定開始標記為 start,結束標記為 end
Regex rx = new Regex("(?<=(" + start + "))[.\\s\\S]*?(?=(" + end + "))", RegexOptions.Multiline | RegexOptions.Singleline);
//搜尋符合的值
MatchCollection matches = rx.Matches(text);
resule += String.Format("共有 {0} 個符合!", matches.Count) + "<br>";
foreach (Match match in matches)
{
string word = match.Value;
int index = match.Index;
resule += String.Format("「{0}」字串索引值為 {1}", word, index) + "<br>";
}
return resule;
}