c#Regex.使用多个匹配结果匹配问题
我正在尝试使用Regex.Matches,它的工作方式似乎与我使用其他语言(如PHP)的方式不同。 以下是我试图做的: 我想从特定网页获取所有表单,但当我尝试执行以下操作时c#Regex.使用多个匹配结果匹配问题,c#,regex,C#,Regex,我正在尝试使用Regex.Matches,它的工作方式似乎与我使用其他语言(如PHP)的方式不同。 以下是我试图做的: 我想从特定网页获取所有表单,但当我尝试执行以下操作时 String pattern = "(?i)<form[^<>]*>(.*)<\\/form>"; MatchCollection matches = Regex.Matches(content, pattern ); foreach (
String pattern = "(?i)<form[^<>]*>(.*)<\\/form>";
MatchCollection matches = Regex.Matches(content, pattern );
foreach (Match myMatch in matches)
{
MessageBox.Show(myMatch.Result("$1"));
}
String pattern=“(?i)(*)”;
MatchCollection matches=Regex.matches(内容、模式);
foreach(在匹配中匹配myMatch)
{
MessageBox.Show(myMatch.Result($1));
}
此代码不显示任何内容,即使该页面上有三个表单。似乎当我使用(.*)时,它会跳过所有内容,直到内容结束。在正则表达式的主要部分尝试类似的方法:
String pattern = "<form[\\d\\D]*?</form>";
String pattern=“默认情况下,Regex
类使
运算符不匹配。\r\n请尝试替换此项:
MatchCollection matches = Regex.Matches(content, pattern );
与:
或
preg_match_all(“~(?isU)(.*)”,$subject,$matches);
.NET没有与PCRE的ungreedy模式等效的模式。这对我来说很有效,但c#只返回所有匹配项(单个匹配项),而不是所有匹配项,这真的很奇怪,除非你试图“欺骗”它。是的,我在php中使用了isU选项。谢谢你的澄清。
MatchCollection matches = Regex.Matches(content, pattern, RegexOptions.Singleline);
string pattern = @"(?is)<form[^<>]*>(.*?)</form>";
preg_match_all('~<form[^<>]*>(.*)</form>~isU', $subject, $matches);
preg_match_all('~(?isU)<form[^<>]*>(.*)</form>~', $subject, $matches);