Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex - match character which is not escaped

Tags:

python

regex

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.

like image 573
bcoughlan Avatar asked Aug 05 '12 18:08

bcoughlan


1 Answers

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

Explanation:

(?<!\\)    Matches if the preceding character is not a backslash
(?:\\\\)*  Matches any number of occurrences of two backslashes
,          Matches a comma
like image 107
MRAB Avatar answered Oct 16 '22 13:10

MRAB