简体   繁体   中英

C# Regex Ignoring Escaped Character

I have a string like this that is delimited | and can contain any character in between:

"one two|three four five|six \| seven eight|nine"

I'd like to find a regex that returns:

one two
three four five
six | seven eight
nine

I can think about how I want to do this but, I don't know regex well enough. I basically want to match until I reach a | that is not preceded by a \\ . How do I do this? I know there is a back tracker, but I don't know how to do it.

Essentially you want to find instances of this pattern:

@"([^|\\]|\\.)+"

This matches:

  1. [^|\\\\] — Any character aside from a pipe or a backslash.
  2. \\\\. — Any character escaped with a backslash.
  3. (...|...)+ — One or more of the preceding (escaped) characters.

The \\\\. construct is nice because it lets you escape any character, in particular other backslashes. This lets you have a backslash at the end of a string, for example:

"backslash \\|forward slash /|pipe \|"
Regex.Split(input, @"(?<!\\)\|");
  • (?<!\\\\) - negative lookbehind. There is no preceding \\

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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