473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Regular expression confusion

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
Sep 24 '06 #1
4 7212

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

Sep 24 '06 #2
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
Sep 24 '06 #3

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

Sep 24 '06 #4
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>

Sep 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
9
by: www.douglassdavis.com | last post by:
I am using the preg_match function (in PHP) that uses perl regular expressions. Apparently I don't really understand regular expressions though. Could some one explain this? If this is the...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
7
by: Kevin CH | last post by:
Hi, I'm currently running into a confusion on regex and hopefully you guys can clear it up for me. Suppose I have a regular expression (0|(1(01*0)*1))* and two test strings: 110_1011101_ and...
5
by: Cylix | last post by:
I am going to write a function that the search engine done. in search engine, we may using double quotation to specify a pharse like "I love you", How can I using regular expression to sperate...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
8
by: sri_san | last post by:
Hello, I am trying to write a regular expresssion which validates against the URL and the textfield has 'http://' prefilled. I either want a valid URL or just leave the 'http://' as it is. I...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.