寫法1:
private void GetRegexStr(string reString)
{
//注意 reString 請替換為需要處理的字符串
string regexCode = "<li data-value=\"(.*?)\" title=\"(.*?)\" >\\s*<a href=\"#\" style=\"background:url\\((.*?)_40x40q90.jpg\\) center no-repeat;\">";
reg = new System.Text.RegularExpressions.Regex(regexCode);
System.Text.RegularExpressions.MatchCollection mc = reg.Matches(reString);
for (int i = 0; i < mc.Count; i++)
{
string temp = mc[i].Groups[1].Value;
string temp2= mc[i].Groups[2].Value;
string temp3 = mc[i].Groups[3].Value;
Console.WriteLine("skuid:"+temp);
Console.WriteLine("顏色分類:"+temp2);
Console.WriteLine("網址:"+temp3);
}
//需要獲取匹配的數據,請遍歷strList 通常情況下(正則表達式中只有一個分組),只需要取strList[1]即可. 如果有多個分組,依次類推即可.
}
寫法2
/*調用方法: 直接粘貼內容至Code中,調用GetRegexStr("這里填寫要處理的字符串")*/
System.Text.RegularExpressions.Regex reg;//正則表達式變量
/// <summary>
/// 正則表達式獲取文本結果
/// </summary>
/// <param name="reString">請替換為需要處理的字符串</param>
/// <returns>處理結果</returns>
private List<string> GetRegexStr(string reString)
{
//注意 reString 請替換為需要處理的字符串
List<string> strList = new List<string>();
string regexCode = "<li data-value=\"(.*?)\" title=\"(.*?)\" >\\s*<a href=\"#\" style=\"background:url\\((.*?)_40x40q90.jpg\\) center no-repeat;\">";
reg = new System.Text.RegularExpressions.Regex(regexCode);
System.Text.RegularExpressions.MatchCollection mc = reg.Matches(reString);
for (int i = 0; i < mc.Count; i++)
{
GroupCollection gc = mc[i].Groups; //得到所有分組
for (int j = 1; j < gc.Count; j++) //多分組 匹配的原始文本不要
{
string temp = gc[j].Value;
if (!string.IsNullOrEmpty(temp))
{
strList.Add(temp); //獲取結果 strList中為匹配的值
}
}
}
//需要獲取匹配的數據,請遍歷strList 通常情況下(正則表達式中只有一個分組),只需要取strList[1]即可. 如果有多個分組,依次類推即可.
return strList;
}
---恢復內容結束---
