473,503 Members | 11,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

re quiz

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
8 970
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2622
by: Sketcher | last post by:
Hi, I am trying to create a quiz, Code is as follows: <html> <head> <title>Quiz</title> </head> <BODY> <Center><TABLE cellSpacing=3 cellPadding=0 border=0>
5
5732
by: Vandana Rola | last post by:
Hello Everyone, I am a beginner in Javascript.I want to create fun quiz tool using javascript (no database). The feature of that tool is every question has five choices. The user needs to select...
4
7455
by: DAL | last post by:
I want to build my kid a program that cycles through questions (using a label for the question), and lets him choose one of two radio buttons for the right answer. How do I get every set of...
0
1641
by: philip | last post by:
hello, i am now developing a quiz application for my school using ASP.NET and SQL SERVER 2005, here is a senario: It will have 20 students for taking a quiz in a classroom, they have to answer...
2
3412
by: kenny | last post by:
I'm making a quiz to be posted on the internet. but I'm facing difficulties in finding a simple timer for the quiz (unlimited timing) which will keep on running regardless to the change of the page...
0
4568
NoPeasHear
by: NoPeasHear | last post by:
I don't know what I am doing wrong... I used this tutorial... http://www.permadi.com/tutorial/flashMXQuiz/index.html It works with their quiz.xml file, but when I add an option for multiple...
3
4388
by: Raqueeb Hassan | last post by:
Hello, I was helping one of my friend's school on setting up a online quiz system. They have the AMP systems to host php+mysql. The quiz script/software should have the following features: a....
1
3070
by: korr | last post by:
Hi there, i'm trying to develop a quiz in flash. Searching on the net, I found a quiz in flashkit from sephiroth.it by Alessandro Crugnola. His quiz has a script that puts the questions and the...
5
2156
nomad
by: nomad | last post by:
Hello Everyone: Just want to ask how easy would it be to build a quiz in Java. I have not use Java for a few months (5). Quiz would need the following: 1. T or F and mulitiple question, possible...
3
2206
by: empiresolutions | last post by:
I am building a app that creates quizzes. This is how it goes - - Create Quiz - Provide up to 10 different types of Quiz Results - Give up to 50 Questions - Each Question has up to 10 possible...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.