Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.021 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'patterns': 0.04; 'matches': 0.07; 'sub': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; "'+'": 0.16; 'repetition': 0.16; 'repetitions': 0.16; 'two,': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'skip:p 40': 0.19; '>>>': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; 'rid': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'order.': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'included': 0.31; "skip:' 10": 0.31; '>>>>': 0.31; 'though.': 0.31; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'sequence': 0.36; 'skip:[ 10': 0.38; 'that,': 0.38; 'how': 0.40; 'times': 0.62; 'skip:n 10': 0.64; 'occur': 0.65; 'to:addr:gmail.com': 0.65; '20,': 0.68; 'skip:r 40': 0.68; 'skip:r 30': 0.69; 'groups.': 0.74; "it'd": 0.84; 'greedy': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=Eg5ihL6Y6yb9Gu5Tm5EggB1f1MMJoyExxUi0IknNhJs=; b=IuWYbyzybwrcSgrsOYJAAjfkqEpOve7V45p6/oar0kj5yhawnuY8sPj1SrjS5VxKUU t4C7irizfaiG3JpVvmIM0bn2v06fcVXsVwKacHVGwVZH4gLms+FqSv3rPaFvYD4F1aMA kNrhwow93vdXO9hiF/uZgNly7iggt8x27u9ONuWvUEyF/NUOfS+CjcJj4G67Td0SKuvS R+n3jUJ0akYg30+7Nc9vgq6Ok2Ziczy1gWLfXMpf8y1yTfHdU5ZL8fv/WVxM9Gy7s2rW 29dpXznL1HEx5SFXxa/DDhcVpiRGZ+eLoZ4a/tyVqwPzZchzFp8cVUeCiLqaKLvgVOFD QCrg== X-Received: by 10.140.18.142 with SMTP id 14mr25169794qgf.105.1390216222839; Mon, 20 Jan 2014 03:10:22 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: From: Devin Jeanpierre Date: Mon, 20 Jan 2014 03:09:42 -0800 Subject: Re: regex multiple patterns in order To: km Content-Type: text/plain; charset=UTF-8 Cc: "comp.lang.python" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390216232 news.xs4all.nl 2865 [2001:888:2000:d::a6]:39583 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64356 On Mon, Jan 20, 2014 at 2:44 AM, km wrote: > I am trying to find sub sequence patterns but constrained by the order in > which they occur > 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. > 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 want to include the '+' in the parens so that repetitions are included in the match, but you still want to group CAA etc. together; for that, you can use non-capturing groups. I don't see how TA could ever match two, though. It'd match once as-is, or thrice if you make the repetition greedy (get rid of the ?s). >>> p = re.compile('((?:CAA)+?)((?:TCT)+?)((?:TA)+?)') >>> p.findall('CAACAACAATCTTCTTCTTCTTATATA') [('CAACAACAA', 'TCTTCTTCTTCT', 'TA')] -- Devin