Where's the error?
Brian McErlean
b_mcerlean at yahoo.com
Tue Dec 4 09:54:06 EST 2001
"Bror Johansson" <bror.johansson at avionics.saab.se> wrote in message news:<9uibrc$199$1 at newstoo.ericsson.se>...
> I have tried, retried, and retried again to understand why Python 2.1 tells
> me this:
>
> Traceback (most recent call last):
> File "D:\Src\Python\renamevobtag.py", line 12, in getlsvobresults
> regline =
> re.compile('^(\*)?\s+\\([^\s]+)\s+\\\\([^\s]+)\s+(p(rivate)|(ublic))\s*$')
> File "d:\python21\lib\sre.py", line 90, in compile
> return _compile(pattern, flags)
> File "d:\python21\lib\sre.py", line 136, in _compile
> raise error, v # invalid expression
> error: unbalanced parenthesis
>
> when approaching this line:
>
> regline =
> re.compile('^(\*)?\s+\\([^\s]+)\s+\\\\([^\s]+)\s+(p(rivate)|(ublic))\s*$')
>
> Can anyone help me clear my eyesight?
>
> I can of course switch over to 'split', but I want to get rid of the extra
> backslashes.
>
> /BJ
When using regexps, remember that python interprets "\" characters for
the strings, so by the time it reaches re.compile, the string reads:
'^(*)?s+\([^s]+)s+\\([^s]+)s+(p(rivate)|(ublic))s*$'
The first \\ has become a single \ before the "(", which is treated as
a literal "(" for the regex, rather than a group, hence the unbalanced
parameters.
The easiest way to fix it is to use rawstrings. Prepend an "r" before
the string to bypass python's string escaping.
ie.
re.compile(r'^(\*)?\s+\\([^\s]+)\s+\\\\([^\s]+)\s+(p(rivate)|(ublic))\s*$')
this should match lines like: r"* \abc123! \\xyz private
...anything..."
Hope this helps.
Brian McErlean
More information about the Python-list
mailing list