Regex - a match character that is not escaped

I am trying to make a regex to match unscreened characters with a comma in a string.

The rule I'm looking for is "a comma that is 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 using a negative look-behind: (?<!(\\+)), , but Python gives me error: look-behind requires fixed-width pattern .

+8
python regex
source share
1 answer

Try this regular expression: (?<!\\)(?:\\\\)*,

Explanation:

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

All Articles