Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Vlastimil Brom Newsgroups: comp.lang.python Subject: Re: one more question on regex Date: Fri, 22 Jan 2016 21:10:44 +0100 Lines: 45 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de Ub3AuHtus2rEJoukuE1R1QKXaKwQVrLYtzPPtiark/jQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'repeated': 0.07; 'subject:question': 0.08; 'tuple': 0.09; 'python': 0.10; 'jan': 0.11; '2016': 0.16; "[('a',": 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'search:': 0.16; 'string': 0.17; 'pointed': 0.18; '>>>': 0.20; '%s"': 0.22; 'seems': 0.23; 'consistent': 0.23; 'matching': 0.23; 'import': 0.24; 'header:In- Reply-To:1': 0.24; 'example': 0.26; 'fri,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'print': 0.30; 'url:mailman': 0.30; 'url:python': 0.33; 'url:listinfo': 0.34; '(for': 0.34; 'previous': 0.34; 'received:google.com': 0.35; 'url:org': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'hi,': 0.38; 'why': 0.39; 'rather': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'your': 0.60; 'subject:more': 0.61; '>>>>>': 0.66; 'captures': 0.84; 'to:name:python': 0.84 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:to :content-type; bh=Nh59scwdbr+mgCTQRvedHxHhSWmHlkF9/UT1VmUIXR4=; b=EV47KNlOujt8oMOsGUZMTuC+dKQ9fYKjYIFJJRQDOOXPZrWjefkTvU4GhlFRk+E7im 6zW9DHQywe9xpYvYwjeQf21WeHFUWROSGz2wA2Jza1kj4+93nQIicO5JRNTGW7KPz9Iw FUPCqluidqIyO/7YEPvZX/Koys5KCZVWGrXmNQlo55Op++gXzo/to7G3V+lMM40cXzGX piy6aKCp2ziIvKEcwzf49WyBasoKlgmYaHVP0QxLmwwSXESnEG+DXjZyNl3r6WwAdW1R 1m7B/Ycvz4NrTyBfGmsc54019tz3YH+Y07SaanFNL471SVz8gGYIU3yBDic7z9lC20+r M/TQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=Nh59scwdbr+mgCTQRvedHxHhSWmHlkF9/UT1VmUIXR4=; b=XCwUmcHhaLJfnLY1gFwVr2dmneMyc63lDcFhX11hKSQpnNmZFxN77txR/u49FoB64V +yomRVsKf/y3mJD9OvFIIUGE80WAX+NAjkYPFW6foEc85bRpCLNonyhWVxt+1ajiFFAT 0qfZlG5B9tU4A9iHdVYnsYkeTNtj9ml614oFNFd7eDLGaSsao0midOpBlpi7v+C5oivO 2ZSRR+Cur0La6iPyxYemDiYKJk/8jAQT2ZDeV6G6fuP5pKbAmgiSABi2/gsu/N99dBaQ pFx+fpSuifn7i7IiH+U6zqKewpKkMqxDYUPP8hP7BQ96LtFQR6PAHSRip+ZCzMSGtJBR dofg== X-Gm-Message-State: AG10YOSp2671UEY+fMHriD3cLCKlvBtUFAQW6iRS5Qfrq6xcUz0WG24Pwl0z+dDBoxUhV2DRZftnoRMyDKt4Fg== X-Received: by 10.112.133.37 with SMTP id oz5mr1964306lbb.27.1453493444631; Fri, 22 Jan 2016 12:10:44 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102025 2016-01-22 16:50 GMT+01:00 mg : > Il Fri, 22 Jan 2016 15:32:57 +0000, mg ha scritto: > >> python 3.4.3 >> >> import re re.search('(ab){2}','abzzabab') >> <_sre.SRE_Match object; span=(4, 8), match='abab'> >> >>>>> re.findall('(ab){2}','abzzabab') >> ['ab'] >> >> Why for search() the match is 'abab' and for findall the match is 'ab'? > > finditer seems to be consistent with search: > regex = re.compile('(ab){2}') > > for match in regex.finditer('abzzababab'): > print ("%s: %s" % (match.start(), match.span() )) > ... > 4: (4, 8) > > -- > https://mail.python.org/mailman/listinfo/python-list Hi, as was already pointed out, findall "collects" the content of the capturing groups (if present), rather than the whole matching text; for repeated captures the last content of them is taken discarding the previous ones; cf.: >>> re.findall('(?i)(a)x(b)+','axbB') [('a', 'B')] >>> (for multiple capturing groups in the pattern, a tuple of captured parts are collected) or with your example with differenciated parts of the string using upper/lower case: >>> re.findall('(?i)(ab){2}','aBzzAbAB') ['AB'] >>> hth, vbr