regexp question

Rami Chowdhury rami.chowdhury at gmail.com
Fri Nov 6 23:10:00 CET 2009


On Fri, 06 Nov 2009 13:50:16 -0800, Jabba Laci <jabba.laci at gmail.com>  
wrote:

> Hi,
>
> How to find all occurences of a substring in a string? I want to
> convert the following Perl code to Python.
>
> Thanks,
>
> Laszlo
>
> ==========
>
> my $text = '<a href="ad1">sdqs</a><a href="ad2">sds</a><a  
> href=ad3>qs</a>';
>
> while ($text =~ m#href="?(.*?)"?>#g)
> {
>    print $1, "\n";
> }
> # output:
> #
> # ad1
> # ad2
> # ad3

Your regular expression pattern should work unchanged, and you probably  
want to use  http://docs.python.org/library/re.html#re.findall or similar  
to do the actual matching. If all you want to do is iterate over the  
matches, I would use re.finditer :-)

-- 
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)



More information about the Python-list mailing list