<p>Let's say I want to represent <code>\q</code> (or any other particular "backslash-escaped character").  That is, I want to match <code>\q</code> but not <code>\\q</code>, since the latter is a backslash-escaped backslash followed by a <code>q</code>.  Yet <code>\\\q</code> would match, since it's a backslash-escaped backslash followed by a backslash-escaped <code>q</code>.  (Well, it would match the <code>\q</code> at the end, not the <code>\\</code> at the beginning.)</p>

<p>I know I need a negative lookbehind, but they always tie my head up in knots, especially since the backslashes themselves have to be escaped in the regexp.</p>

[{'answer_id': 56583, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://www.codinghorror.com/blog/archives/001016.html" rel="nofollow noreferrer">Now You Have Two Problems</a>.</p>\n\n<p>Just write a simple parser. If the regex ties your head up in knots now, just wait a month.</p>\n'}, {'answer_id': 56668, 'author': 'Jared', 'author_id': 1980, 'author_profile': 'https://Stackoverflow.com/users/1980', 'pm_score': -1, 'selected': False, 'text': '<p>The best solution to this is to do your own string parsing as Regular Expressions don\'t really support what you are trying to do. (rep @Frank Krueger if you go this way, I\'m just repeating his advice)</p>\n\n<p>I did however take a shot at a exclusionary regex. This will match all strings that do not fit your criteria of a "\\" followed by a character.</p>\n\n<pre><code>(?:[\\\\][\\\\])(?!(([\\\\](?![\\\\])[a-zA-Z])))\n</code></pre>\n'}, {'answer_id': 56671, 'author': 'Leon Timmermans', 'author_id': 4727, 'author_profile': 'https://Stackoverflow.com/users/4727', 'pm_score': 5, 'selected': True, 'text': "<p>Updated:\nMy new and improved Perl regex, supporting more than 3 backslashes:</p>\n\n<pre>/(?&lt;!\\\\)    # Not preceded by a single backslash\n  (?>\\\\\\\\)* # an even number of backslashes\n  \\\\q       # Followed by a \\q\n  /x;</pre>\n\n<p>or if your regex library doesn't support extended syntax.</p>\n\n<pre>/(?&lt;!\\\\)(?>\\\\\\\\)*\\\\q/</pre>\n\n<p>Output of my test program:</p>\n\n<pre>q does not match\n\\q does match\n\\\\q does not match\n\\\\\\q does match\n\\\\\\\\q does not match\n\\\\\\\\\\q does match</pre>\n\n<p>Older version</p>\n\n<pre>/(?:(?&lt;!\\\\)|(?&lt;=\\\\\\\\))\\\\q/</pre>\n"}, {'answer_id': 56842, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 2, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/a/56671/6761181">Leon Timmermans</a> got exactly what I was looking for.  I would add one small improvement for those who come here later:</p>\n\n<pre><code>/(?&lt;!\\\\)(?:\\\\\\\\)*\\\\q/\n</code></pre>\n\n<p>The additional <code>?:</code> at the beginning of the <code>(\\\\\\\\)</code> group makes it not saved into any match-data.  I can\'t imagine a scenario where I\'d want the text of that saved.</p>\n'}]
['https://Stackoverflow.com/questions/56554', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1190/']