Search code examples
c#regex

Regex split where \# splits and \\# does not


I need to split some text but \\ means a single \ and \ is an indicator for a literal character. I have started with the following code but it is fooled by \\. so \# (# preceded by 0 or an even number of 's ) would split but # (# preceded by odd number of 's ) would not split

string message = @"1#2\\#3#3\#still 3#4\\\# still 4";
var v = Regex.Split(message, @"(?<!\\)#")
        .ToArray();

I would like the message to come out as

1
2\\
3\#still 3
4\\\# still 4

but cannot figure out how to ignore the \\ instances so mine comes out as

1
2\\#3
3\#still 3
4\\\# still 4

Solution

  • It will be easier to do this with Matches than with Split, as you can then pair as many escaping slashes with the subsequent character as needed:

        var v = Regex.Matches(message, @"(\\.|[^#])+")
                      .Cast<Match>()
                      .Select(m => m.Value)
                      .ToArray();
    

    Make sure to include using System.Linq;