reusing parts of a string in RE matches?
Bo Yang
struggleyb at gmail.com
Wed May 10 11:13:57 EDT 2006
John Salerno 写道:
> I probably should find an RE group to post to, but my news server at
> work doesn't seem to have one, so I apologize. But this is in Python
> anyway :)
>
> So my question is, how can find all occurrences of a pattern in a
> string, including overlapping matches? I figure it has something to do
> with look-ahead and look-behind, but I've only gotten this far:
>
> import re
> string = 'abababababababab'
> pattern = re.compile(r'ab(?=a)')
>
Below from the python library reference
*|(?=...)|*
Matches if ... matches next, but doesn't consume any of the string.
This is called a lookahead assertion. For example, Isaac (?=Asimov)
will match |'Isaac '| only if it's followed by |'Asimov'|.
> m = pattern.findall(string)
>
> This matches all the 'ab' followed by an 'a', but it doesn't include the
> 'a'. What I'd like to do is find all the 'aba' matches. A regular
> findall() gives four results, but really there are seven.
>
>
I try the code , but I give seven results !
> Is there a way to do this with just an RE pattern, or would I have to
> manually add the 'a' to the end of the matches?
>
> Thanks.
>
More information about the Python-list
mailing list