Combing two regex expressions to keep escaped characters but exclude non-escaped characters

Viewed 16

For example, I have a string "aout\>>>out\\nt\\<foo<<bar" I'm having trouble finding regex that would find words:

  • "aout\>"

  • "out\\nt\\<foo"

  • "bar"

I am trying to exclude any '<' or '>' or '|' characters that are not escaped with '\'.

Right now, I have [^ \t\n]+ to give me all "words" without <>|, but I want to exclude the part of the string that has the expression (?:(?<!\\)[<>|])+ (any non-negated <>|). Is there a way to combine these two expressions?

1 Answers

You can use

(?:\\[<>|]|[^\s<>|])+

See the regex demo. Details:

  • (?: - start of a non-capturing group:
    • \\[<>|] - a \ char and then a <, > or | char
    • | - or
    • [^\s<>|] - any char other than a whitespace, <, > and |
  • )+ - end of the non-capturing group, one or more repetitions.
Related