RegEx for matching a character in a string only if it isn't escaped

mazz0 :

How can I find and operate on a character in a string only if it isn't escaped (ie proceeded by an odd number of another character)?

Example:

Desired character: |

Escape character: \

| should be found (and operated on, for example split)

\| should not

\\| should

\\\| should not

revo :

Use a negative a lookbehind to define a boundary:

(?<!\\)(?:\\\\)*\|

See live demo here

Taking care of backslashes in Java, above regex would be:

(?<!\\\\)(?:\\\\\\\\)*\\|

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=143449&siteId=1