c#正则匹配取出文本内容 循环输出
生活随笔
收集整理的這篇文章主要介紹了
c#正则匹配取出文本内容 循环输出
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
寫法1:
private void GetRegexStr(string reString) { //注意 reString 請?zhí)鎿Q為需要處理的字符串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);} //需要獲取匹配的數(shù)據(jù),請遍歷strList 通常情況下(正則表達式中只有一個分組),只需要取strList[1]即可. 如果有多個分組,依次類推即可.}寫法2
/*調用方法: 直接粘貼內容至Code中,調用GetRegexStr("這里填寫要處理的字符串")*/System.Text.RegularExpressions.Regex reg;//正則表達式變量/// <summary>/// 正則表達式獲取文本結果/// </summary>/// <param name="reString">請?zhí)鎿Q為需要處理的字符串</param>/// <returns>處理結果</returns>private List<string> GetRegexStr(string reString){//注意 reString 請?zhí)鎿Q為需要處理的字符串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中為匹配的值}}}//需要獲取匹配的數(shù)據(jù),請遍歷strList 通常情況下(正則表達式中只有一個分組),只需要取strList[1]即可. 如果有多個分組,依次類推即可.return strList;}
?
---恢復內容結束---
轉載于:https://www.cnblogs.com/chunge123/p/7978756.html
總結
以上是生活随笔為你收集整理的c#正则匹配取出文本内容 循环输出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 循序渐进开发WinForm项目(5)--
- 下一篇: i74790k配什么主板