re quiz

David C. Ullrich dullrich at sprynet.com
Thu Jun 12 06:57:28 EDT 2008


True or False? (no fair looking it up)

(*) If repl is a string then re.sub(pattern, repl, s) 
returns s with non-overlapping occurences of pattern 
replaced by repl.

I assumed it was true - spent a few hours trying to
figure out what was going on with a certain re.sub,
then noticed that (*) is false:

(**) "If repl is a string, any backslash escapes in it are 
processed. That is, "\n" is converted to a single newline 
character, "\r" is converted to a linefeed, and so forth."

So I changed my r"\remark{Hint}" to r"\\remark{Hint}"
and things were fine.

A pointless question and then a practical one.

Pointless question: There must be a good reason for (**).
What would it be? Seems needlessly confusing to me (of
course a lot of things seem confusing to me...)

Maybe it's going to be confusing no matter what they do.
But "\\n" looks like it doesn't contain a newline, but it
gets converted to something that does.

(Another fascinating question is how they could phrase
the docs here so as to confuse nobody. Because "\n"
_is_ a newline, or so it's going to look to many people;
I'd spell it out:: "a string containing '\' followed by 'n' ".)

Practical question: What's a _complete_ list of the
escapes included in the "and so forth" in (**)?

(Or is there a function somewhere that will convert
r"\remark{Hint}" to r"\\remark{Hint}" for me, and
do the same for precisely the escpapes referred to
in the "and so forth"?)


David C. Ullrich



More information about the Python-list mailing list