[Regex] Search and replace?

Sion Arrowsmith siona at chiark.greenend.org.uk
Thu Nov 13 05:57:14 EST 2008


Gilles Ganault  <nospam at nospam.com> wrote:
>#Extract two bits, and rewrite the HTML
>person = re.compile('<tr onMouseOver=(?P<item1>.+?)>.+?<a onmouseover="Tip(?P<item2>.+?) </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 -- siona at 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



More information about the Python-list mailing list