By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,538 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,538 IT Pros & Developers. It's quick & easy.

re quiz

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Jun 12, 8:57 pm, David C. Ullrich <dullr...@sprynet.comwrote:
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:
Well, the docs do say "Return the string obtained by replacing the
leftmost non-overlapping occurrences of pattern in string by the
replacement repl." -- care to tell us what "a certain re.sub" is, and
false in what way?
Jun 27 '08 #2

P: n/a
David C. Ullrich wrote:
(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"?)
I think you just have to escape the backslash:

re.sub(pattern, replacement_string.replace("\\", "\\\\"), s)

Anyway here's the list of troublemakers:
>>def means_trouble(c):
.... try:
.... return re.sub("x", "\\" + c, "x") != "\\" + c
.... except:
.... return True
....
>>[c for c in map(chr, range(256)) if means_trouble(c)]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\\', 'a', 'b', 'f', 'g', 'n', 'r', 't', 'v']
Peter

Jun 27 '08 #3

P: n/a
David C. Ullrich <du******@sprynet.comwrote:
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"?)
As the documentation says:

Character escapes
Numbered Groups: \0 \1 \2 \3 ...
Named groups: \g<name>
Numbered groups with explicit termination of the number: \g<0\g<1...

But it doesn't matter what the complete list is. All of the escapes start
with \ so doubling all the \\ will prevent any of them being interpreted as
special so if you aren't wanting to substitute any groups into the string
just try repl.replace('\\', r'\\')
--
Duncan Booth http://kupuguy.blogspot.com
Jun 27 '08 #4

P: n/a
On Thu, 12 Jun 2008 05:12:55 -0700 (PDT), John Machin
<sj******@lexicon.netwrote:
>On Jun 12, 8:57 pm, David C. Ullrich <dullr...@sprynet.comwrote:
>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:

Well, the docs do say "Return the string obtained by replacing the
leftmost non-overlapping occurrences of pattern in string by the
replacement repl."
That's the _first sentence_, yes. I _quoted_ another sentence
(from an old version, istr it phrased slightly differently in
recent versions) in my post.
-- care to tell us what "a certain re.sub" is, and
false in what way?
Read the OP.
David C. Ullrich
Jun 27 '08 #5

P: n/a
On Thu, 12 Jun 2008 14:12:31 +0200, Peter Otten <__*******@web.de>
wrote:
>David C. Ullrich wrote:
>(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"?)

I think you just have to escape the backslash:

re.sub(pattern, replacement_string.replace("\\", "\\\\"), s)

Anyway here's the list of troublemakers:
Heh - I shoulda thought of that, determining which
ones cause trouble by checking to see which ones
cause trouble. Thanks.
>>>def means_trouble(c):
... try:
... return re.sub("x", "\\" + c, "x") != "\\" + c
... except:
... return True
...
>>>[c for c in map(chr, range(256)) if means_trouble(c)]
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\\', 'a', 'b', 'f', 'g', 'n', 'r', 't', 'v']
Peter
David C. Ullrich
Jun 27 '08 #6

P: n/a
On 12 Jun 2008 12:32:13 GMT, Duncan Booth
<du**********@invalid.invalidwrote:
>David C. Ullrich <du******@sprynet.comwrote:
>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"?)

As the documentation says:

Character escapes
Numbered Groups: \0 \1 \2 \3 ...
Named groups: \g<name>
Numbered groups with explicit termination of the number: \g<0\g<1...
Right - I was wondering about a complete list of character
escapes.
>But it doesn't matter what the complete list is. All of the escapes start
with \ so doubling all the \\ will prevent any of them being interpreted as
special so if you aren't wanting to substitute any groups into the string
just try repl.replace('\\', r'\\')
Good point.

David C. Ullrich
Jun 27 '08 #7

P: n/a
David C. Ullrich schrieb:
>-- care to tell us what "a certain re.sub" is, and
false in what way?

Read the OP.
Well, aren't you funny. Maybe you should have referenced the other
thread so one can find the OP?

Regards,
Johannes

--
"Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
reicht zu wissen, daß andere es besser können und andere es auch
besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
in de.sci.electronics <47***********************@news.freenet.de>
Jun 27 '08 #8

P: n/a
In article <qv************@joeserver.homelan.net>,
Johannes Bauer <df***********@gmx.dewrote:
David C. Ullrich schrieb:
-- care to tell us what "a certain re.sub" is, and
false in what way?
Read the OP.

Well, aren't you funny. Maybe you should have referenced the other
thread so one can find the OP?
What other thread? OP is sometimes Original Poster and
sometimes Original Post. In the original post in this
very thread I gave an quote from the docs and an example
illustrating the answer to the question I was asked.

Ok, I guess it's hard to find the top of the thread.
I wanted to replace a certain pattern with r"\remark{Hint}".
I didn't understand why that didn't work until I read
the bit of the docs that I quoted in my original post:

(**) "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."
Regards,
Johannes
--
David C. Ullrich
Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.