Regex and Octal Characters

I am trying to write a regular expression that captures octal characters.

For example, if the line that I am comparing with my regular expression looks like this:

char x = '\077';

I want my regular expression to capture '\077'

I tried to do this through a re-module and a regular expression of the form:

"'\\[0-7]{1-3}'"

But this does not reflect the octal character. How can octal characters be identified using regex in Python?

Edit:

As an example of what I mean, consider the C code:

char x = '\077'; 
printf("%c", x);

I would like to write '\077'from the first line.

Edit:

, , . , r , .

, :

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

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

'\077', : char = '\077';

.

+4
3

:

>>> str = r"char x = '\077'; \nprintf(\"%c\", x);"

r .

:

>>> print re.findall(ur"'\\[0-7]{1,3}'", str)
["'\\077'"]

- RegEx


stdin regex:

#!/usr/bin/python
import sys
import re

str = sys.stdin.read()
print re.findall(ur"'\\[0-7]{1,3}'", str)
+2

, .

, :

s = r"char x = '\077';"
codes = re.findall(r"\\([0-7]{3})", s)

, :

characters = [chr(int(c, 8)) for c in codes]

bytes (Python 3):

bytes_result = bytes(characters)

:

str_result = ''.join(characters)
+1
import re
x="""char x = '\077';
  printf("%c", x);"""


print oct(ord(re.findall(r"'([^']*)'",x)[0]))

: 077

, 077, \077, python ?, \077 . ?, octal.

0

Source: https://habr.com/ru/post/1623973/


All Articles