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 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
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
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
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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
| |