简体   繁体   English

c# Regex.Matches 多个匹配结果的问题

[英]c# Regex.Matches problems with multiple matches results

I am trying to use Regex.Matches and it seems to work in a different way to what I am used to with other languages like PHP.我正在尝试使用 Regex.Matches,它的工作方式似乎与我习惯使用 PHP 等其他语言的方式不同。 Here is what I am trying to do:这是我正在尝试做的事情:

I want to get all forms from a particular webpage, but when I try to do the following我想从特定网页获取所有 forms,但是当我尝试执行以下操作时

        String pattern = "(?i)<form[^<>]*>(.*)<\\/form>"; 
        MatchCollection matches = Regex.Matches(content, pattern );

        foreach (Match myMatch in matches)
        {
            MessageBox.Show(myMatch.Result("$1"));
        }

This code does not show anything even though there are three forms on that page.即使该页面上有三个 forms,此代码也不会显示任何内容。 It seems that when I use (.*) it just skips everything till the end of the content.似乎当我使用 (.*) 时,它只是跳过所有内容,直到内容结束。

The Regex class makes the .正则Regex class 使. operator NOT match \r and \n by default.运算符默认匹配 \r 和 \n。 Try replacing this:尝试替换这个:

MatchCollection matches = Regex.Matches(content, pattern );

with:和:

MatchCollection matches = Regex.Matches(content, pattern, RegexOptions.Singleline);

Try something like this for the main portion of your Regex:为您的正则表达式的主要部分尝试这样的事情:

    String pattern = "<form[\\d\\D]*?</form>";

It is a pattern I am currently using to strip all tags of a specific type out of a document, but should do well finding the form tags.这是我目前用来从文档中删除特定类型的所有标签的模式,但应该很好地找到表单标签。 You can alter the \d\D section, if so desired.如果需要,您可以更改 \d\D 部分。

string pattern = @"(?is)<form[^<>]*>(.*?)</form>"; 

That regex should work the same in PHP and C# (or, more accurately, PCRE and .NET).该正则表达式在 PHP 和 C#(或者更准确地说,PCRE 和 .NET)中的工作方式应该相同。 If you're getting minimal matches in PHP without the ?如果您在没有? , you probably have the /U ("ungreedy") option set, eg: ,您可能设置了/U (“不贪婪”)选项,例如:

preg_match_all('~<form[^<>]*>(.*)</form>~isU', $subject, $matches);

or或者

preg_match_all('~(?isU)<form[^<>]*>(.*)</form>~', $subject, $matches);

.NET has no equivalent for PCRE's ungreedy mode. .NET 没有 PCRE 的非贪婪模式的等价物。

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

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