regular expression match collection

A

ajikoe

Hello,

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 ?

Sincerely Yours,
Pujo Aji
 
F

Fredrik Lundh

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 ?
.... print m.span()
....
(5, 7)
(8, 10)
(11, 13)

</F>
 
S

Steve Holden

Hello,

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 ?

Sincerely Yours,
Pujo Aji
You need re.findall() (or, equivalently, the findall() method of an re).

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,730
Latest member
scavoli

Latest Threads

Top