Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'instance': 0.05; 'expressions.': 0.09; 'pm,': 0.11; '>>>': 0.12; 'wrote:': 0.14; 'assertion': 0.16; 'captured': 0.16; 'duplicates': 0.16; 'regexp': 0.16; 'subject:Extracting': 0.16; '\xa0print': 0.16; '\xa0to': 0.16; 'code.': 0.18; 'skip:r 30': 0.19; 'cheers,': 0.20; 'code': 0.22; 'header:In-Reply-To:1': 0.22; 'posted': 0.22; 'extract': 0.25; "i'm": 0.26; 'instead': 0.26; 'object': 0.27; 'message- id:@mail.gmail.com': 0.28; 'fri,': 0.29; 'probably': 0.30; 'group:': 0.31; 'received:209.85.210.46': 0.31; 'received:mail- pz0-f46.google.com': 0.31; 'import': 0.32; 'to:addr:python-list': 0.32; 'another': 0.32; 'expression': 0.33; 'regular': 0.34; 'question': 0.35; 'some': 0.37; 'received:209.85': 0.37; 'skip:- 10': 0.37; 'apr': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'question,': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'received:209': 0.39; 'how': 0.39; 'header:Received:5': 0.40; '2011': 0.62; 'candide': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=zGfKFY2smcOKYmET2lqM1qxYQzCtSjW5MU/bZJ3MSZo=; b=BKfTaOHwjHizsxv654owm/3amxmjDC7MY8Js9O0oWC9iY3xjtWsqrbCeZ35jrgV5Hf lStcOoY9OxtDNxqwiJz1gKHL28LWEzSVO0rT8J3BVzjAReTrSBHe8xpuy1wSBAq6UYkY dZ+5XLr8CIlh+cZYLgXnloyPsroOxWYYpCoNc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=cftSS13stv/OUWcJv5/qZyrQrt6UpZbFaMy1FERq2negTyVgycfv5F7OoeJe3vC4yS F65wun9BEL14vm4m6vQP83a/OHSXSPseW/6qypYixZzcN1ILSsquMsx09SN1WoZ8YOYO GDdNMNhq/b0SVkgKxNG5W4lLwfd0Er4OEV2dc= MIME-Version: 1.0 In-Reply-To: <4d963bfa$0$1584$426a34cc@news.free.fr> References: <4d963bfa$0$1584$426a34cc@news.free.fr> From: Ian Kelly Date: Fri, 1 Apr 2011 16:42:51 -0600 Subject: Re: Extracting repeated words To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 50 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1301697810 news.xs4all.nl 81474 [::ffff:82.94.164.166]:43981 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:2412 On Fri, Apr 1, 2011 at 2:54 PM, candide wrote: > Another question relative to regular expressions. > > How to extract all word duplicates in a given text by use of regular > expression methods ? =A0To make the question concrete, if the text is > > ------------------ > Now is better than never. > Although never is often better than *right* now. > ------------------ > > duplicates are : > > ------------------------ > better is now than never > ------------------------ > > Some code can solve the question, for instance > > # ------------------ > import re > > regexp=3Dr"\w+" > > c=3Dre.compile(regexp, re.IGNORECASE) > > text=3D""" > Now is better than never. > Although never is often better than *right* now.""" > > z=3D[s.lower() for s in c.findall(text)] > > for d in set([s for s in z if z.count(s)>1]): > =A0 =A0print d, > # ------------------ > > but I'm in search of "plain" re code. You could use a look-ahead assertion with a captured group: >>> regexp =3D r'\b(?P\w+)\b(?=3D.+\b(?P=3Ddup)\b)' >>> c =3D re.compile(regexp, re.IGNORECASE | re.DOTALL) >>> c.findall(text) But note that this is computationally expensive. The regex that you posted is probably more efficient if you use a collections.Counter object instead of z.count. Cheers, Ian