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

后端 未结 1 693
清歌不尽
清歌不尽 2021-01-22 15:40

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:

相关标签:
1条回答
  • 2021-01-22 16:28

    Use a negative a lookbehind to define a boundary:

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

    See live demo here

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

    (?<!\\\\)(?:\\\\\\\\)*\\|
    
    0 讨论(0)
提交回复
热议问题