gpt4 book ai didi

c# - 正则表达式适用于 .NET 测试站点,但不适用于 C# 环境

转载 作者:太空宇宙 更新时间:2023-11-03 23:23:32 24 4
gpt4 key购买 nike

引用这个stackoverflow问题:- Regex Pattern help: finding HTML pattern when nested ASP.NET Eval?

我在这里收到了问题的答案:- regexstorm link

适用于正则表达式 .NET 测试站点的 .NET 答案不适用于我的 C# Visual Studio 环境。这是它的单元测试:

[Test]
public void GetAllHtmlSubsectionsWorksAsExpected()
{
var regPattern = new Regex(@"(?'o'<)(.*)(?'-o'>)+");

var html =
"<%@ Page Language=\"C#\" %>" +
"<td class=\"c1 c2 c3\" colspan=\"2\">" +
"lorem ipsum" +
"<div class=\"d1\" id=\"div2\" attrid=\"<%# Eval(\"CategoryID\") %>\">" +
"testing 123" +
"</div>" +
"asdf" +
"</td>";

List<string> results = new List<string>();

MatchCollection matches = regPattern.Matches(html);
for (int mnum = 0; mnum < matches.Count; mnum++)
{
Match match = matches[mnum];
results.Add("Match #" + (mnum + 1) + " - Value: " + match.Value);
}

Assert.AreEqual(5, results.Count()); //Fails: results.Count() == 1
}

为什么这在 regexstorm 网站上有效,但在我的单元测试中却无效?

最佳答案

请注意,使用正则表达式解析 HTML 不是最佳实践,您应该使用专用的解析器。

现在,至于问题本身,您使用的模式仅适用于具有 1 个以 < 开头的单个子字符串的并以相应的 > 结尾.但是,您的输入字符串没有换行符!它看起来像:

<%@ Page Language="C#" %><td class="c1 c2 c3" colspan="2">lorem ipsum<div class="d1" id="div2" attrid="<%# Eval("CategoryID") %>">testing 123</div>asdf</td>

.*子模式称为贪心点匹配模式,它匹配除换行符以外的尽可能多的字符(因为它抓取整行然后回溯 查看是否找到下一个子模式(此处为 > ),因此您将获得最后一个可能的 > )。

要解决这个问题,您需要一个适当的平衡结构匹配模式:

<((?>[^<>]+|<(?<c>)|>(?<-c>))*(?(c)(?!)))>

参见 regex demo

C#:

var r = new Regex(@"
< # First '<'
( # Capturing group 1
(?> # Atomic group start
[^<>] # Match all characters other than `<` or `>`
|
< (?<c>) # Match '<', and add a capture into group 'c'
|
> (?<-c>) # Match '>', and delete 1 value from capture stack
)*
(?(c)(?!)) # Fails if 'c' stack isn't empty!
)
> # Last closing `>`
"; RegexOptions.IgnoreWhitespace);

免责声明:如果您取消配对 <,即使这个正则表达式也会失败或 >在您的元素节点中,这就是为什么不使用正则表达式来解析 HTML

关于c# - 正则表达式适用于 .NET 测试站点,但不适用于 C# 环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34563469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com