Replacing "\"s using re.sub 
Author Message
 Replacing "\"s using re.sub

I need to replace all instances of "\" in a string with "\\". The
function "re.sub" looks like the way to do it. "re.sub" is called
by "re.sub(pattern, repl, string)". For my problem what should
"pattern" and "repl" be?

Thanks,
  Edward C. Jones



Wed, 07 Aug 2002 03:00:00 GMT  
 Replacing "\"s using re.sub

Quote:
> I need to replace all instances of "\" in a string with "\\". The
> function "re.sub" looks like the way to do it. "re.sub" is called
> by "re.sub(pattern, repl, string)". For my problem what should
> "pattern" and "repl" be?

If you're only dealing with a substitution of one exact string with
another exact string, it's faster and easier to use string.replace:

S2 = string.replace(S, '\\', '\\\\')

If you must use regular expressions for some reason (sigh), think
about the problem a bit.  "pattern" needs to match a single \ ; \ is
used as a special character in regular expressions, so to match a
single \ the pattern has to be \\ .

You want the replacement to be \\, but \ is also special in
replacement strings for things like \g<2>, so you need to double them
both.

The patterns need to be inside strings, and python also uses \ as a
special character; use a raw string, as in r"contents", to prevent
Python from paying attention to \..

S = "String with \\ in it; here's another \\ "
S2 = re.sub(r'\\', r'\\\\', S)
print S
print S2

This outputs:

String with \ in it; here's another \
String with \\ in it; here's another \\

If you didn't use a raw string, you'd need yet another level of \ quoting,
doubling all the backslashes again:

S2 = re.sub('\\\\', '\\\\\\\\', S)

--
A.M. Kuchling                   http://starship.python.net/crew/amk/
Sending a newgroup message without permission of Leader Kibo: Poster is forced
to adopt twelve wacky sitcom children.
    -- Kibo, in the Happynet Manifesto



Wed, 07 Aug 2002 03:00:00 GMT  
 Replacing "\"s using re.sub

Quote:

> I need to replace all instances of "\" in a string with "\\". The
> function "re.sub" looks like the way to do it. "re.sub" is called
> by "re.sub(pattern, repl, string)". For my problem what should
> "pattern" and "repl" be?

If you're looking to *replace* one *string* with another, why not go
for string.replace?
--

INTERNET: Learn what you know.
Share what you don't.


Wed, 07 Aug 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. replace string AFTER "size","initial", "next"

2. What does this match: sub(/^.*\(/,"")

3. "sub vi calling main vi"

4. Hi, this code: text0 = "One $BLAH Three" text1 = "One @BLAH Three" text0.sub!("$BLAH", "Two") text1.sub!("@BLAH", "Two") print text0,"\n" print text1,"\n" produces thiHi, this code: text0 = "One $BLAH Three" text1 = "One @BLAH Three" text0.sub!("$BLAH", "T

5. Regex backreference in method "sub"

6. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

7. Using regsub to replace "\" with "\\".

8. awk "search and replace"

9. What replaces "EXIT"?

10. Replacing "Shutdown" Action Button in LaunchPad

11. Rookie question: "Replace"-command?

12. Replace with regular expressions "except if..."

 

 
Powered by phpBB® Forum Software