Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 3个可能的结尾应该使用什么正则表达式模式_C#_Regex - Fatal编程技术网

C# 3个可能的结尾应该使用什么正则表达式模式

C# 3个可能的结尾应该使用什么正则表达式模式,c#,regex,C#,Regex,这是一个令人烦恼的问题。我有一段文字来自一份无法比较的脚本报告 Picture Compare Produced: 10/17/2012 9:42:25 AM Ignoring Unimportant Left file: K:\HDA_FIN\user\JMan\All\A-0001.jpg Right file: K:\HDA_FIN\user\JMan\All\B-0001.jpg 3454945 same pixel(s) 2154 ignored unimportant dif

这是一个令人烦恼的问题。我有一段文字来自一份无法比较的脚本报告

Picture Compare
Produced: 10/17/2012 9:42:25 AM
Ignoring Unimportant
Left file: K:\HDA_FIN\user\JMan\All\A-0001.jpg     Right file: K:\HDA_FIN\user\JMan\All\B-0001.jpg
3454945 same pixel(s)
2154 ignored unimportant difference pixel(s)
2741 important difference pixel(s)
当脚本比较文件夹中匹配的JPEG时,此操作会重复多次。但是有些JPEG是100%相同的,因此它们没有被忽略的不重要或重要的差异。有些会有相同的差异和重要的差异,但没有不重要的,等等。因此,我试图捕获以“图片比较”开始,以下一个“图片比较”再次开始之前的最后一个“像素”结束的匹配

我所尝试的

我没有做的是一个丑陋的方法:我使用流阅读器,而!在EndOfStream中,我执行sr.ReadLine()并将每一行添加到列表中。然后,我使用for循环遍历列表并应用一系列if语句来确定循环中的当前字符串和前面的几个字符串是否匹配我要查找的内容,如果匹配,我将它们绑定到一个对象。但Regex肯定要简单得多

    var lineByLine = new List<string>();
    while (!sr.EndOfStream)
    {
        string line = sr.ReadLine();
        sb.AppendLine(line);
        if (line.Trim().Length > 0)  // && !line.Contains("picture-report layout"))
        {
            lineByLine.Add(line);
        }
    }

    Contents = sb.ToString();

    //get the report blocks


    for (int i = 0; i < lineByLine.Count; i++)
    {
        Block block;
        string[] lines = { "", "", "", "", "", "", "" };

        //does line contain pic compare? if so, this is the start of an object
        if (lineByLine[i].Contains("Picture Compare"))
        {
            lines[0] = lineByLine[i]; //start line
            block = new Block();
            lines[1] = lineByLine[i + 1]; //produces
            lines[2] = lineByLine[i + 2]; //subheading
            if (lineByLine[i + 3].Contains("Left"))
            {
                lines[3] = lineByLine[i + 3]; //file
                if (lineByLine[i + 4].Contains("same pixel(s)"))
                {
                    lines[4] = lineByLine[i + 4]; //same
                    if (lineByLine[i + 5].Contains("ignored unimportant"))
                    {
                        lines[5] = lineByLine[i + 5];
                        if (lineByLine[i + 6].Contains(" important difference"))
                        {
                            lines[6] = lineByLine[i + 6];
                        }
                    }
                }
                else if (lineByLine[i + 4].Contains("ignored unimportant"))
                {
                    lines[5] = lineByLine[i + 4];
                    if (lineByLine[i + 5].Contains(" important difference"))
                    {
                        lines[6] = lineByLine[i + 5];
                    }
                }
                else if (lineByLine[i + 4].Contains(" important difference"))
                {
                    lines[6] = lineByLine[i + 4];
                }
            }
            Blocks.Add(new Block(lines[0], lines[1], lines[2], lines[3], lines[4], lines[5], lines[6]));
        }
    }

}
finally
{
    sr.Close();
}

-但在所有情况下,它都停止在相同的像素上。我需要更贪婪的东西。有什么想法吗?

你可以试着找到下一个起点,而不是找到终点:

@"Picture Compare(?:(?!Picture Compare).)*"
这将匹配
图片比较
,然后匹配尽可能多的字符,只要它们不开始新的
图片比较
(这就是负前瞻的目的)。这应该只是给你所有这些块


然后,在每个块上,您可以进行更简单的扫描,以获得您感兴趣的值(不幸的是,我不知道哪些值是,否则我可能也会为这些值使用另一个正则表达式
:p
)。

尝试使用正则表达式模式

Picture Compare\n?(?:(?!Picture Compare)[^\n]*\n?)*

因此,您阅读了第
行图片比较
以及以下所有不以
图片比较

开头的行,您真正想要的值是什么?每个匹配应该是一个以图片比较开始并在下一个图片比较之前结束的块。每个块可以有5、6或7行,其中字符串有一个行终止符。此外,您应该将选项设置为多行,而不是单线。多行仅返回第一行图片比较,而不是整个块。这与直觉相反,但在这个多行字符串中,单线是最合适的。奇怪,就是这样,谢谢。负前瞻现在在我的实用地带。再次感谢。是的,在每个块中,我可以解析每一行,比如提取用于确定应用于每对图像的差异百分比的数字。谢谢正向前瞻也可以工作“(图片比较)(.*)(=图片比较| \\Z)”@Gebb捕捉得好!对于正则表达式引擎来说,这可能相当于同样的工作,但值得注意的是,这也很方便!Thx@GebbThis不像m那样对我有效。布特纳的解决方案,我很好奇为什么不。“我要好好考虑一下。”格雷福克斯374——m的解。布特纳很慢,一个字符接一个字符。我的是一行接一行,速度更快,工作正常-参见
Picture Compare\n?(?:(?!Picture Compare)[^\n]*\n?)*