Regular expression returning an index where match was found?

J

Jon Nicoll

Hi All
I would like to be able to use regular expression searches to
determine the index(es) within a string where a match was found. ie.,
given

import re

def myFunc():
# to be written ;-)

the call

myfunc("abc", "abcdefabcabc")

would return (say) a list [0, 6, 9]

I can't see a straightforward way of doing this from reading the
Regular Expression HOWTO and the python docs; any
pointers/suggestions?

Thanks
Jon N
 
J

James Henderson

Hi All
I would like to be able to use regular expression searches to
determine the index(es) within a string where a match was found. ie.,
given

import re

def myFunc():
# to be written ;-)

the call

myfunc("abc", "abcdefabcabc")

would return (say) a list [0, 6, 9]

I can't see a straightforward way of doing this from reading the
Regular Expression HOWTO and the python docs; any
pointers/suggestions?

Thanks
Jon N

I belive you want the start() and end() methods of a match object. They are
in the docs:

http://www.python.org/doc/current/lib/match-objects.html

James
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top