Finding all regex matches by index?

R

Roy Smith

I have a long string (possibly 100s of Mbytes) that I want to search for
regex matches. re.finditer() is *almost* what I want, but the problem
is that it returns matching strings. What I need is a list of offsets
in the string where the regex matched. Thus:

s = "this is a string"
find("is", s) => [2, 5]

Is there anything that does this?
 
R

Roy Smith

Roy Smith said:
I have a long string (possibly 100s of Mbytes) that I want to search for
regex matches. re.finditer() is *almost* what I want, but the problem
is that it returns matching strings. What I need is a list of offsets
in the string where the regex matched. Thus:

s = "this is a string"
find("is", s) => [2, 5]

Is there anything that does this?

Ugh, looks like I simply mis-read the docs. findall() returns strings,
finditer() returns match objets (which is what I need). Duh.
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top