backslash plague

Paul McGuire ptmcg at
Fri Oct 22 21:43:30 CEST 2004

"Luis P. Mendes" <luisXX_lupe2XX at> wrote in message
news:2tt4b2F24cffeU1 at
> 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
> 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

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 )

['R0', '1.2644', '1.2344', 'D']

-- Paul

More information about the Python-list mailing list