backslash plague

Paul McGuire ptmcg at austin.rr._bogus_.com
Fri Oct 22 15:43:30 EDT 2004


"Luis P. Mendes" <luisXX_lupe2XX at netvisaoXX.pt> wrote in message
news:2tt4b2F24cffeU1 at uni-berlin.de...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> hi,
>
> I've already read many pages on this but I'm not able to separate the
> string 'R0\1.2646\1.2649\D' in four elements, using the \ as the
separator.
>
> a='R0\1.2644\1.2344\D'
> re.sub(r'\'','ff',a)  does nothing
> and why must I write two '' after the \?  If I hadn't used r I would
> understand...
>
> how should I do it?
>
> Luis

Problem 1:
Did you perhaps intend:
    a = r'R0\1.2644\1.2344\D'
Otherwise, your string contains this: "R0?.2644?.2344\D" - the \1 sequence
is interpreted to mean "ascii character 1", which is the ASCII <SOH>.

Problem 2: "why must I write two '' after the \?"
Even raw strings cannot handle a backslash as the final character, as this
is interpreted as being an escaped quotation character.  Your assignment to
a above is a good candidate for raw strings, but futile for '\\' etc.  (See
next.)

Problem 3:
If you are trying to re.sub the delimiting backslashes, then you need to
double-double them for re to process them correctly.

Try this:

a = r'R0\1.2644\1.2344\D'
bslash = '\\'
re_bslash = '\\\\'
print a.split( bslash )
print re.sub( re_bslash, 'ff', a )

gives:
['R0', '1.2644', '1.2344', 'D']
R0ff1.2644ff1.2344ffD


-- Paul





More information about the Python-list mailing list