How to use (?! ...) a regular expression pattern to skip the whole unsurpassed part?

I would like to use the regex pattern ((?!(SEPARATOR)).)*to split the string.

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        var separator = "__";
        var pattern = String.Format("((?!{0}).)*", separator);
        var regex = new Regex(pattern);

        foreach (var item in regex.Matches("first__second"))
            Console.WriteLine(item);        
    }
}

It works great when it SEPARATORis the only character, but when it is longer than 1 character, I get an unexpected result. In the above code, the second line matches "_second" instead of "second". How can I change my template to skip the whole unique separator?

My real problem is line splitting where I have to skip line separators inside quotes. My line separator is not a predefined value and may be, for example, "\ r \ n".

+4
2

- :

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "plum--pear";
      string pattern = "-";            // Split on hyphens

      string[] substrings = Regex.Split(input, pattern);
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}


// The method displays the following output:
//    'plum'
//    ''
//    'pear'  
+1

.NET , . PCRE (*SKIP)(*FAIL), .NET. , PCRE.NET, .NET regex Regex.Split

, , , [anything here],

var res = Regex.Split(s, @"\[[^][]*]").Where(m => !string.IsNullOrEmpty(m));

, __, String.Split.

, , , ,

var res = Regex.Matches(s, "(?:\"[^\"]*\"|[^\r\n\"])+")
    .Cast<Match>()
    .Select(m => m.Value)
    .ToList();

regex

1+ (- +) ", 0+, ", " ( "[^"]*") (|) any char CR, LF / " (. [^\r\n"]).

0

Source: https://habr.com/ru/post/1681352/


All Articles