简体   繁体   中英

regex - match character which is not escaped

I'm trying to make a regex to match unescaped comma characters in a string.

The rule I'm looking for is "A comma not preceded by an even number of backslashes".

Test cases:

True    abc,abc
False   abc\,abc
True    abc\\,abc
False   abc\\\,abc
True    abc\\\\,abc
False   abc\\\\\,abc

I tried to use a negative look-behind: (?<!(\\\\+)), but Python gives me error: look-behind requires fixed-width pattern .

Try this regex: (?<!\\\\)(?:\\\\\\\\)*,

Explanation:

(?<!\\)    Matches if the preceding character is not a backslash
(?:\\\\)*  Matches any number of occurrences of two backslashes
,          Matches a comma

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