[英]c# Regex.Matches problems with multiple matches results
我正在尝试使用 Regex.Matches,它的工作方式似乎与我习惯使用 PHP 等其他语言的方式不同。 这是我正在尝试做的事情:
我想从特定网页获取所有 forms,但是当我尝试执行以下操作时
String pattern = "(?i)<form[^<>]*>(.*)<\\/form>";
MatchCollection matches = Regex.Matches(content, pattern );
foreach (Match myMatch in matches)
{
MessageBox.Show(myMatch.Result("$1"));
}
即使该页面上有三个 forms,此代码也不会显示任何内容。 似乎当我使用 (.*) 时,它只是跳过所有内容,直到内容结束。
正则Regex
class 使.
运算符默认不匹配 \r 和 \n。 尝试替换这个:
MatchCollection matches = Regex.Matches(content, pattern );
和:
MatchCollection matches = Regex.Matches(content, pattern, RegexOptions.Singleline);
为您的正则表达式的主要部分尝试这样的事情:
String pattern = "<form[\\d\\D]*?</form>";
这是我目前用来从文档中删除特定类型的所有标签的模式,但应该很好地找到表单标签。 如果需要,您可以更改 \d\D 部分。
string pattern = @"(?is)<form[^<>]*>(.*?)</form>";
该正则表达式在 PHP 和 C#(或者更准确地说,PCRE 和 .NET)中的工作方式应该相同。 如果您在没有?
,您可能设置了/U
(“不贪婪”)选项,例如:
preg_match_all('~<form[^<>]*>(.*)</form>~isU', $subject, $matches);
或者
preg_match_all('~(?isU)<form[^<>]*>(.*)</form>~', $subject, $matches);
.NET 没有 PCRE 的非贪婪模式的等价物。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.