regular expression match collection

Fredrik Lundh fredrik at pythonware.com
Fri Feb 4 09:23:39 EST 2005


<ajikoe at gmail.com> wrote:

> For example I have a string : "Halo by by by"
> Then I want to take and know the possition of every "by"
> how can I do it in python?
>
> I tried to use:
>
>  p = re.compile(r"by")
>  m = p.search("Helo by by by")
>  print m.group() # result "by"
>  print m.span()  # result (5,7)
>
> How can I get the information of the other by ?

>>> import re
>>> p = re.compile("by")
>>> for m in p.finditer("Helo by by by"):
...     print m.span()
...
(5, 7)
(8, 10)
(11, 13)

</F> 






More information about the Python-list mailing list