Error invoking Bogus when running regex

I got help yesterday with regex matching which worked fine as standalone. But when you enter this code, I get a "false exit" error. Below is the code and trace. Could you please point me to what I am doing wrong?

#!/usr/bin/env python
import re

sf = open("a.txt","r")
out = open("b.txt","w")
regex = re.compile(r'Merging\s+\d+[^=]*=\s*\'\w+@\w+\x\w+\'\\"')

      

for line in sf: m = regex.findall(line) for i in m: print >> out,line,

Tracking:

Traceback (last call last):   File "match.py", line 6, in <module> regex = re.compile(r'Merging\s+\d+[^=]*=\s*\'\w+@\w+\x\w+\'\\"') File "/usr/lib/python2.7/re.py", line 190, in compile return _compile(pattern, flags) File "/usr/lib/python2.7/re.py", line 242, in _compile raise error, v # invalid expression sre_constants.error: bogus escape: '\\x'

+3


source to share


2 answers


\x

is not a valid special sequence. If you want to match a literal \x

, you need to escape the backslash with \\x

, or if you need something else, use valid one , for example with \w

.

This will compile:



re.compile(r'Merging\s+\d+[^=]*=\s*\'\w+@\w+\\x\w+\'\\"')

      

+3


source


\x

must be followed by a hexadecimal value (i.e. exactly two hexadecimal digits):

>>> '\x61'
'a'
>>> '\x'
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape

      



If you want to match a literal \x

, you can escape the backslash so that the: is x

not escaped \\x

.

+1


source







All Articles