.NET正则表达式负前瞻 - 我究竟做错了什么? [英] .NET Regex Negative Lookahead - what am I doing wrong?

查看:197
本文介绍了.NET正则表达式负前瞻 - 我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有:

StartTest
  NoInclude
EndTest

StartTest
  Include
EndTest

和我使用:

/StartTest(?!NoInclude)[\s\S]*?EndTest/g

为什么我匹配两组

Regexr?例如:的 http://regexr.com/3db8m

Regexr example: http://regexr.com/3db8m

推荐答案

你失败了比赛的前瞻,如果 NoInclude 后直 StartTest 出现。你需要一个回火贪婪令牌

You fail the match with the lookahead if NoInclude appears straight after StartTest. You need a tempered greedy token:

(?s)StartTest(?:(?!(?:Start|End)Test|NoInclude).)*EndTest
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

见的正则表达式演示

正则表达式是匹配 StartTest ,然后匹配这不是<$ C的任何文本$ C> StartTest , EndTest NoInclude ,到 EndTest

The regex is matching StartTest, then matches any text that is not StartTest, EndTest or NoInclude, up to the EndTest.

由于 * 是贪婪的,它将使比赛一样,因为它可以。负前瞻将使停在了后边带有以下替代的位置匹配:

Since the * is greedy, it will make the . match as much as it can. The negative lookahead will make it stop matching at the locations that are followed with the following alternatives:


  • (?:开始|完)测试 - StartTest EndTest

  • NoInclude - 只需 NoInclude

  • (?:Start|End)Test - StartTest or EndTest
  • NoInclude - just NoInclude.

注意:(?S​​)的 是一个内嵌修饰符(相当于 RegexOptions.Singleline 标志),用于修改行为,使其匹配LF(新行),也是一种模式。如果没有这个修饰符(或没有 RegexOptions.Singleline )点的任何字符,但换行符相匹配。

NOTE: The (?s) is an inline modifier (equivalent of RegexOptions.Singleline flag) that modifies the . behavior in a pattern making it match LF (newlines), too. Without this modifier (or without RegexOptions.Singleline) a dot matches any character but a newline.

注2::如果您在本机代码环境外测试一个正则表达式,请确保您正在使用您正则表达式的味道适当的测试仪。 regexr.com只有支持JavaScript的味道,regex101.com支持JS,PCRE和Python的味道,和RegexStorm.net/RegexHero.net支持.NET的味道。周围有很多更多的测试,阅读他们的支持和什么不可以第一。

NOTE2: If you are testing a regex outside of the native code environment, make sure you are using an appropriate tester for your regex flavor. regexr.com only supports JavaScript flavor, regex101.com supports JS, PCRE and Python flavors, and RegexStorm.net/RegexHero.net support .NET flavor. There are many more testers around, read what they support and what not first.

下面是一个的 C#演示

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;
public class Test
{
    public static void Main()
    {
        var input = "StartTest\n  NoInclude\nEndTest\n\nStartTest\n  Include\nEndTest";
        var regex = new Regex(@"(?s)StartTest(?:(?!(?:Start|End)Test|NoInclude).)*EndTest");
        var results = regex.Matches(input).Cast<Match>()
                       .Select(p => p.Value)
                       .ToList();
        Console.WriteLine(string.Join("\n", results));
    }
}

这篇关于.NET正则表达式负前瞻 - 我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆