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


Groups > comp.lang.python > #64355

Re: regex multiple patterns in order

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.017
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'patterns': 0.04; 'matches': 0.07; 'repeated': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'parentheses': 0.16; 'units,': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'first.': 0.19; 'skip:p 40': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'instance,': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'order.': 0.26; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'matching': 0.30; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; '>>>>': 0.31; 'but': 0.35; 'received:google.com': 0.35; 'skip:[ 10': 0.38; 'pm,': 0.38; 'how': 0.40; "you're": 0.61; 'times': 0.62; 'skip:n 10': 0.64; 'occur': 0.65; '20,': 0.68; 'skip:r 40': 0.68; 'skip:r 30': 0.69; 'captures': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Zd8jhCW1iCSY2DShAgkQRdc8lTkvnbMlTPcxUaK3AVA=; b=mxvbKKMuicU5jOMpTzvoVHKbWBFbD82OV05PlMfhVbs3PwQIgMaHAevRI9NzH2Qaqj KEnLqUEo04BbEb7SX55mejiK4llRiNHPMfx/N/eg74oIrQE8zA0ei1vxT1InYnTn2EMN 42T5CtBkIEH+8nOjSwVGigAIOIu+YTlui34qOMMPas+D8bQVW2IhkNijCUvdOFvFT9s/ OzFhhFFOsTj3IBOP0uJfZ53/rYdJxewRhBiegPngpcpXASYUqOwJp6x37bdpr7nejVWI c6nTu+cCC9/+1mMffjyEdLloP1tvdp6+68oIvTmHUC9eqMjzHGVY+fmGyNT7gR4zjV5x a16A==
MIME-Version 1.0
X-Received by 10.66.102.39 with SMTP id fl7mr17728598pab.43.1390216246595; Mon, 20 Jan 2014 03:10:46 -0800 (PST)
In-Reply-To <CAPV1RAAiD3qWqrAJYc4yb80CaHG4A9N4jz4aY_CsyJr_nAUx9Q@mail.gmail.com>
References <CAPV1RAAiD3qWqrAJYc4yb80CaHG4A9N4jz4aY_CsyJr_nAUx9Q@mail.gmail.com>
Date Mon, 20 Jan 2014 22:10:46 +1100
Subject Re: regex multiple patterns in order
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5747.1390216250.18130.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1390216250 news.xs4all.nl 2855 [2001:888:2000:d::a6]:39949
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64355

Show key headers only | View raw


On Mon, Jan 20, 2014 at 9:44 PM, km <srikrishnamohan@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

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


Thread

Re: regex multiple patterns in order Chris Angelico <rosuav@gmail.com> - 2014-01-20 22:10 +1100

csiph-web