C#Regex在两个字符串之间获取字符串,并在两个字符串之间使用通配符字符串?

C#Regex在两个字符串之间获取字符串,并在两个字符串之间使用通配符字符串?,c#,html,regex,tags,wildcard,C#,Html,Regex,Tags,Wildcard,我知道有人以某种身份提出了这个问题,但我还没有看到解决方案的有效示例。我知道有Agility包可以解析HTML字符串,但我不想下载/安装它。我使用 string html = client.DownloadString("http://yoursite.com/page.html"); 我有一个带有类的标记,但其中一些标记也有自己的ID或样式,例如: <td>I Dont want this</td> <td class="myClass">I want t

我知道有人以某种身份提出了这个问题,但我还没有看到解决方案的有效示例。我知道有Agility包可以解析HTML字符串,但我不想下载/安装它。我使用

string html = client.DownloadString("http://yoursite.com/page.html");
我有一个带有类的标记,但其中一些标记也有自己的ID或样式,例如:

<td>I Dont want this</td>
<td class="myClass">I want this</td>
<td class="myClass" id="myID">I want this</td>
<td style="border-top-width: 0px; class="myClass">I want this</td>
我不要这个
我想要这个
我想要这个

这将仅识别应该执行此操作的
(.+)

实例:

<td>I Dont want this</td>
<td class="myClass">I want this</td>
<td class="myClass" id="myID">I want this</td>
<td style="border-top-width: 0px; class="myClass">I want this</td>

但是,如果列表的格式与您描述的不同,则必须从捕获组中排除“”字符

我不得不指出,HTML的一部分将始终具有问题指定的相同格式。浏览器不会自动完成和自动更正任何错误或重载。只需在
属性之前添加另一个
[^>]*
。如果你的HTML和你说的一样一致,那就足够了。
<td class="myClass"[^>]*>(.*?)</td>
 (?is)
 < td                   # 'td' tag, or any tag for that matter
 (?= \s )
 (?>                    # Atomic grouping
      (?:
           (?<= \s )
           class  \s* = \s*       # 'class' attribute
           "
           (?<class>              # 'class' value                                                      
                [^"]*                  
           )
           "
        |  (?<= \s )
           id  \s* = \s*          # 'id' attribute
           "
           (?<id>                 # 'id' value                                                      
                [^"]*
           )
           "
        |  " .*? "
        |  ' .*? '
        |  [^>]*? 
      )+
      >
 )
 (?(class)              # Conditional - Only tags with our 'class' or 'id' attr/value
   |  
      (?(id)
        |  (?!)
      )
 )
 .*? 
 </td \s* >