繁体   English   中英

有没有办法正则表达式多行 html 块?

[英]is there a way to regex multiline html blocks?

它是我的 html 页面的一部分。 我想找到标记之间的所有名称: <a href ... </a></td> 它的多行和“新”关键字每次都有不同的数字。

        <tr class="hl">
        <td class="vil fc">
            <a href="mypage.php?new=4645">
                name                </a>
        </td>

默认情况下, Regex类会搜索整个多行字符串,并且会查找跨越多行的匹配项。 但是,匹配项是否可以跨越多行取决于您的模式。 如果你给它的模式说匹配必须全部在一行上,那么它显然不会返回任何多行匹配。 因此,例如:

Dim input As String = "Canine
Dog
K9
D
o
g
Puppy"
Dim count As Integer = Regex.Matches(input, "Dog").Count 
Dim countMulti As Integer = Regex.Matches(input, "D\s*o\s*g").Count 
Console.WriteLine(count)      ' Outputs "1"
Console.WriteLine(countMulti) ' Outputs "2"

由于\\s*表示任意数量的空格(包括换行符),因此第二个模式将匹配第二个模式,其中每个字母都在自己的行上。

因此,如果它默认有效,并且您正在询问它,我认为真正的问题是您不允许在模式中使用换行符。 因此,例如,这将起作用:

Dim input As String = "<tr class=""hl"">
<td class=""vil fc"">
<a href=""mypage.php?New=4645"">
        name                </a>
</td>"
Dim m As Match = Regex.Match(input, "<a[^>]*>((?:.|\s)*?)</a>")
If m.Success Then
    Dim g As String = m.Groups(1).Value
    Console.WriteLine(g)  ' Outputs vbCrLf & "                name                "
End If

一个常见的假设是. 将匹配任何内容,包括换行符,但通常情况并非如此。 默认情况下, . 只有匹配任何不是新行字符。 如果你想要. 要还包括换行符,您可以通过指定可能会引起混淆的RegexOptions.Singleline选项来实现。 例如,这也有效:

Dim input As String = "<tr class=""hl"">
<td class=""vil fc"">
<a href=""mypage.php?New=4645"">
        name                </a>
</td>"
Dim m As Match = Regex.Match(input, "<a[^>]*>(.*?)</a>", RegexOptions.Singleline)
If m.Success Then
    Dim g As String = m.Groups(1).Value
    Console.WriteLine(g)  ' Outputs vbCrLf & "                name                "
End If

或者,您可以在正则表达式模式本身中指定单行选项,方法是将(?s)放在开头:

Dim m As Match = Regex.Match(input, "(?s)<a[^>]*>(.*?)</a>")

为了解决您在评论中提到的其他问题,如果您只想匹配包含newdid参数的链接,您可以执行以下操作:

<a\s+[^>]*href\s*=[^>]*newdid\s*=[^>]*>(.*?)</a>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM