Newbie question: matching
Robert Brewer
fumanchu at amor.org
Thu Apr 15 19:58:09 EDT 2004
josh R wrote:
> I am trying to write some python to parse html code. I find it easier
> to do in perl, but would like to learn some basic python. My code
> looks like this:
>
> line = "<tr>eat at joe's</tr><tr>or else</tr><tr>you'll starve</tr>"
> so = re.compile("(\<tr\>.*?\<\\tr\>)")
> foo=so.match(line)
> print foo.groups()
>
> I'd like to get an array of elements that looks like this:
>
> array(0)= <tr>eat at joe's</tr>
> array(1)= <tr>or else</tr>
> array(2)= <tr>you'll starve</tr>
>
> Could you please tell me the correct way to do the matching?
>>> import re
>>> line = "<tr>eat at joe's</tr><tr>or else</tr><tr>you'll starve</tr>"
>>> re.findall(r"<tr>.*?</tr>", line)
["<tr>eat at joe's</tr>", '<tr>or else</tr>', "<tr>you'll starve</tr>"]
> also, is there something similiar to perl's s/foo/bar/g?
re.sub(pattern, repl, string)
FuManChu
More information about the Python-list
mailing list