Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Regular expression confusion

Reply
Thread Tools

Regular expression confusion

 
 
York
Guest
Posts: n/a
 
      09-24-2006
I have two backslash - a. and I want to replace them with one backslash,
but I failed:

>>> import re
>>> a = '\\\\'
>>> re.sub(r'\\\\', '\\', '\\\\')

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.3/sre.py", line 143, in sub
return _compile(pattern, 0).sub(repl, string, count)
File "/usr/lib/python2.3/sre.py", line 258, in _subx
template = _compile_repl(template, pattern)
File "/usr/lib/python2.3/sre.py", line 245, in _compile_repl
raise error, v # invalid expression
sre_constants.error: bogus escape (end of line)
>>>


anybody knows why?

Thanks,

York
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      09-24-2006

York wrote:
> I have two backslash - a. and I want to replace them with one backslash,
> but I failed:
>
> >>> import re
> >>> a = '\\\\'
> >>> re.sub(r'\\\\', '\\', '\\\\')

> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "/usr/lib/python2.3/sre.py", line 143, in sub
> return _compile(pattern, 0).sub(repl, string, count)
> File "/usr/lib/python2.3/sre.py", line 258, in _subx
> template = _compile_repl(template, pattern)
> File "/usr/lib/python2.3/sre.py", line 245, in _compile_repl
> raise error, v # invalid expression
> sre_constants.error: bogus escape (end of line)
> >>>

>
> anybody knows why?


Yep. There are *two* levels of escaping happening (1) Python compiler
(2) re compiler (in the first two args, but of course only Python in
the 3rd).
To get your single backslash you need to start out with four cooked or
two raw:

| >>> re.sub(r'\\\\', '\\\\', '\\\\')
'\\'
| >>> re.sub(r'\\\\', r'\\', '\\\\')
'\\'

Cheers,
John

 
Reply With Quote
 
 
 
 
York
Guest
Posts: n/a
 
      09-24-2006
Oh, that's right, the second arg is escaped by re compiler too.

Thank you, John.

York



John Machin wrote:
> York wrote:
>
>>I have two backslash - a. and I want to replace them with one backslash,
>>but I failed:
>>
>> >>> import re
>> >>> a = '\\\\'
>> >>> re.sub(r'\\\\', '\\', '\\\\')

>>Traceback (most recent call last):
>> File "<stdin>", line 1, in ?
>> File "/usr/lib/python2.3/sre.py", line 143, in sub
>> return _compile(pattern, 0).sub(repl, string, count)
>> File "/usr/lib/python2.3/sre.py", line 258, in _subx
>> template = _compile_repl(template, pattern)
>> File "/usr/lib/python2.3/sre.py", line 245, in _compile_repl
>> raise error, v # invalid expression
>>sre_constants.error: bogus escape (end of line)
>> >>>

>>
>>anybody knows why?

>
>
> Yep. There are *two* levels of escaping happening (1) Python compiler
> (2) re compiler (in the first two args, but of course only Python in
> the 3rd).
> To get your single backslash you need to start out with four cooked or
> two raw:
>
> | >>> re.sub(r'\\\\', '\\\\', '\\\\')
> '\\'
> | >>> re.sub(r'\\\\', r'\\', '\\\\')
> '\\'
>
> Cheers,
> John
>

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      09-24-2006

York wrote:
> Oh, that's right, the second arg is escaped by re compiler too.


No, that's wrong, you [should] do the escaping, the compiler unescapes


Cheers,
John

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      09-24-2006
York wrote:

> I have two backslash - a. and I want to replace them with one backslash,
> but I failed:
>
> >>> import re
> >>> a = '\\\\'
> >>> re.sub(r'\\\\', '\\', '\\\\')


John has already sorted the RE-specific part of the problem, but it's
also worth noting that using the RE engine for literal strings is over-
kill; an ordinary replace is easier to use and faster:

>>> a = "\\\\"
>>> a.replace("\\\\", "\\")

'\\'

</F>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Regular Expression confusion Christie Taylor Perl Misc 13 10-31-2004 07:03 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments