Regex and Octal Characters
<p>I'm attempting to write a regex that captures octal characters.</p>

<p>For example, if the line I'm comparing to my regex is:</p>

<pre><code>char x = '\077';
</code></pre>

<p>I want my regex to capture <code>'\077'</code></p>

<p>I attempted to do this via the re module and a regex of the form:</p>

<pre><code>"'\\[0-7]{1-3}'"
</code></pre>

<p>But this doesn't capture the octal character.  How can octal characters be identified using regex in Python?</p>

<p>Edit:</p>

<p>As an example of what I mean, consider the C code:</p>

<pre><code>char x = '\077'; 
printf("%c", x);
</code></pre>

<p>I would like to capture <code>'\077'</code> from the first line.</p>

<p>Edit:</p>

<p>After testing some of the suggestions in this thread, I have a case that works.  I realize that after adding the octal regex to a larger regex, I needed to prefix with r for raw input, or escape each backslash, for a total of four backslashes.</p>

<p>For example, both of these solve the problem:</p>

<pre><code>regex = re.compile(r"\s*("                  
                        r"'\\0[0-7]{1,2}'"          # octal
                        "|[a-zA-Z_][a-zA-Z_\d]*"    # identifer
                        ")")
regex.findall(line)
</code></pre>

<p>and</p>

<pre><code>regex = re.compile(r"\s*("                  
                        "'\\\\0[0-7]{1,2}'"         # octal
                        "|[a-zA-Z_][a-zA-Z_\d]*"    # identifer
                        ")")
regex.findall(line)
</code></pre>

<p>Which will produce <code>'\077'</code> if line is: <code>char = '\077';</code></p>

<p>Thanks for the help everyone.</p>