Yesterday, I got help with the regex matching which worked well as a standalone. But when put into this code, I am getting the "bogus escape error". The code and traceback are below. 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,
The traceback is:
Traceback (most recent 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'
\x
is not a valid special sequence. If you want to match a literal \x
, you need to escape the backslash using \\x
or if you need something else, use a valid one, such as you did with \w
.
This will compile:
re.compile(r'Merging\s+\d+[^=]*=\s*\'\w+@\w+\\x\w+\'\\"')
\x
must be followed by a hex value (i.e. exactly two hex 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
then you can escape the backslash so that the x
is not being escaped: \\x
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With