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

[Regex] Search and replace?

P: n/a
Hello

I need to iterate through a variable, and for each pattern that
matches, replace this with something else.

I read the chapter in www.amk.ca/python/howto/regex/, but the output
is wrong:

=======
#Extract two bits, and rewrite the HTML
person = re.compile('<tr onMouseOver=(?P<item1>.+?)>.+?<a
onmouseover="Tip(?P<item2>.+?)&nbsp;</td>')

output = person.sub('<tr onMouseOver=\1><td><a
onmouseover="Tip\2</td>', input)
=======

Does someone have a simple example handy so I can check what's wrong
with the above?

Thank you.
Nov 13 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Gilles Ganault <no****@nospam.comwrote:
>#Extract two bits, and rewrite the HTML
person = re.compile('<tr onMouseOver=(?P<item1>.+?)>.+?<a onmouseover="Tip(?P<item2>.+?)&nbsp;</td>')

output = person.sub('<tr onMouseOver=\1><td><a onmouseover="Tip\2</td>', input)

Does someone have a simple example handy so I can check what's wrong
with the above?
Do you have an example of the input string which you're using? And
exactly how the output is "wrong"? At a wild guess, you mean your
substitution string to be:

'<tr onMouseOver=\\1><td><a onmouseover="Tip\\2</td>' or
r'<tr onMouseOver=\1><td><a onmouseover="Tip\2</td>'

(note the backslash escaping).

Oh, and don't use "input" as a name -- you're shadowing the builtin
input function.

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Nov 13 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.