C# .net 正则表达式 提取 <ul class="lists3">下的 li a href的值

2025-06-21 21:41:05
推荐回答(1个)
回答1:

using System;
using System.Text.RegularExpressions;
public class Test {
    private static void ABC(string str, Regex[] rs, int index) {
            if(index>rs.Length-1){
                return;
            }
            Regex r=rs[index];
            Match m = r.Match(str);
            while (m.Success) {
                if(index == rs.Length - 1){
                    Console.WriteLine(m.Groups[1]);
                }
                for (int i = 0; i < m.Groups.Count; i++) {
                    Group group = m.Groups[i];
                    string vv=group.Value;
                    ABC(vv, rs, index+1);
                }
                m = m.NextMatch();
            }
    }
    public static void Main() {
        string str = "ss
    \r\n
  • \r\nabc\r\n
  • \r\n
\r\n\r\n
  • dcac
  • \r\n\r\n
  • dcac
  • \r\n\r\n";
            Regex r1 = new Regex("(?i)]+class[=\"\'\\s]+lists3[\"\']?[^>]*>(?:(?!<\\/ul>)[\\s\\S])+<\\/ul>");
            Regex r2 = new Regex("(?i)]*>(?:(?!<\\/li>)[\\s\\S])+<\\/li>");
            Regex r3 = new Regex("(?i)]+href[=\"\'\\s]+([^\"\'>]*)[\"\']?[^>]*>");
            Regex[] rs = {r1,r2,r3};
            ABC(str, rs, 0);
        }
    }