Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64357

Re: regex multiple patterns in order

From Ben Finney <ben+python@benfinney.id.au>
Subject Re: regex multiple patterns in order
Date 2014-01-20 22:18 +1100
References <CAPV1RAAiD3qWqrAJYc4yb80CaHG4A9N4jz4aY_CsyJr_nAUx9Q@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5748.1390216721.18130.python-list@python.org> (permalink)

Show all headers | View raw


km <srikrishnamohan@gmail.com> writes:

> I am trying to find sub sequence patterns but constrained by the order
> in which they occur

There are also specific resources for understanding and testing regex
patterns, such as <URL:http://www.pythonregex.com/>.

> For example
>
> >>> 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.

Yes, because the grouping operator (the parens ‘()’) in each case
contains exactly “CAA”, “TCT”, “TA”. If you want the repetitions to be
part of the group, you need the repetition operator (in your case, ‘+’)
to be part of the group.

> How can I get 3 matches of CAA, followed by  four matches of TCT followed
> by 2 matches of TA ?

With a little experimenting I get:

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

Remember that you'll get no more than one group returned for each group
you specify in the pattern.

> Well these patterns (CAA/TCT/TA) can occur any number of times and
> atleast once so I have to use + in the regex.

Be aware that regex is not the solution to all parsing problems; for
many parsing problems it is an attractive but inappropriate tool. You
may need to construct a more specific parser for your needs. Even if
it's possible with regex, the resulting pattern may be so complex that
it's better to write it out more explicitly.

-- 
 \     “To punish me for my contempt of authority, Fate has made me an |
  `\                   authority myself.” —Albert Einstein, 1930-09-18 |
_o__)                                                                  |
Ben Finney

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: regex multiple patterns in order Ben Finney <ben+python@benfinney.id.au> - 2014-01-20 22:18 +1100
  Re: regex multiple patterns in order Roy Smith <roy@panix.com> - 2014-01-20 09:52 -0500
    Re: regex multiple patterns in order Neil Cerutti <neilc@norwich.edu> - 2014-01-20 16:04 +0000
    Re: regex multiple patterns in order Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-20 16:16 +0000
    Re: regex multiple patterns in order Devin Jeanpierre <jeanpierreda@gmail.com> - 2014-01-20 08:40 -0800
      Re: regex multiple patterns in order Rustom Mody <rustompmody@gmail.com> - 2014-01-20 09:06 -0800
        Re: regex multiple patterns in order Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-20 17:30 +0000
    Re: regex multiple patterns in order Neil Cerutti <neilc@norwich.edu> - 2014-01-20 17:09 +0000
    Re: regex multiple patterns in order Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-20 17:33 +0000

csiph-web