regex multiple patterns in order

Chris Angelico rosuav at gmail.com
Mon Jan 20 06:10:46 EST 2014


On Mon, Jan 20, 2014 at 9:44 PM, km <srikrishnamohan at gmail.com> wrote:
>>>> p = re.compile('(CAA)+?(TCT)+?(TA)+?')
>>>> p.findall('CAACAACAATCTTCTTCTTCTTATATA')
> [('CAA', 'TCT', 'TA')]
>
> But I instead find only one instance of the CAA/TCT/TA in that order.
> How can I get 3 matches of CAA, followed by  four matches of TCT followed by
> 2 matches of TA ?
> Well these patterns (CAA/TCT/TA) can occur any number of  times and atleast
> once so I have to use + in the regex.

You're capturing the single instance, not the repeated one. It is
matching against all three CAA units, but capturing just the first.
Try this:

>>> p = re.compile('((?:CAA)+)((?:TCT)+)((?:TA)+)')
>>> p.findall('CAACAACAATCTTCTTCTTCTTATATA')
[('CAACAACAA', 'TCTTCTTCTTCT', 'TATATA')]

This groups "CAA" with non-capturing parentheses (?:regex) and then
captures that with the + around it.

ChrisA



More information about the Python-list mailing list