FAQ

Paul Rubin wrote:
I have a string s, possibly megabytes in size, and two regexps, p and q.

I want to find the first occurence of q that occurs after the first
occurence of p.

Is there a reasonable way to do it?

g1 = re.search(p, s)
g2 = re.search(q, s[g1.end():])
q_offset = g1.end() + g2.start()

is not a reasonable way, since it copies a ton of data around
(slicing an arbitrary sized chunk off s into a new temporary string).

Most regexps libs I know of have a way to start the search at a
specified offset. Python's string.find and string.index methods
have a similar optional arg. But I don't see it described in the
re module docs.

Am I missing something?

Thanks.
Can't you just combine the two regexps? for example if p='abc' and
q='stu', can't you compile and match against something like the following:
import re
pq=re.compile(r'abc.*?(stu)')
s=pq.search('aaass_abcsd_stuqwer_stu')
s.start(1)
12
Notice i used .*?, the non greedy match to return the first occurrence
of q after p.

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 3 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedOct 23, '03 at 12:14a
activeOct 23, '03 at 4:32p
posts3
users2
websitepython.org

People

Translate

site design / logo © 2022 Grokbase