regexp search question

Donald 'Paddy' McCarthy paddy3118 at netscape.netNOTthisBIT
Thu Oct 23 12:32:01 EDT 2003



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.





More information about the Python-list mailing list