Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.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.026 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'url:pypi': 0.08; 'brilliant': 0.09; 'received:mail-bw0-f46.google.com': 0.09; '>>>': 0.12; '"|"': 0.16; '...]': 0.16; 'capturing': 0.16; 'parentheses:': 0.16; 'received:209.85.214.46': 0.16; 'subject:regex': 0.16; 'list?': 0.16; 'subject:list': 0.19; 'before.': 0.19; 'compile': 0.19; 'header:In-Reply-To:1': 0.21; 'message-id:@mail.gmail.com': 0.28; 'received:209.85.214': 0.28; 'e.g.': 0.29; 'import': 0.29; 'alternatives': 0.29; 'lists': 0.29; 'pattern': 0.30; 'named': 0.32; 'to:addr:python-list': 0.33; 'list': 0.33; 'starting': 0.33; '...': 0.34; 'however,': 0.34; 'option': 0.35; 'there': 0.35; 'newer': 0.35; 'usual': 0.35; 'received:google.com': 0.37; 'received:209.85': 0.37; 'enhanced': 0.37; 'skip:r 30': 0.37; 'url:python': 0.38; 'hello,': 0.38; 'subject:from': 0.38; 'url:org': 0.38; 'cases,': 0.38; 'subject:: ': 0.38; 'non': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.39; 'more': 0.60; 'show': 0.66; 'alternative': 0.71; 'features:': 0.73; 'supplying': 0.84; 'page;': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=6LBrXTw9fzi+A9XgU2I4Md91MnuYH3GbJ7ATwZkzyt4=; b=AAU54aBLOZPFI5o4AbGE6mfbQys3P3hFvPWJV/qUUPc1ukFyE/SqlsOgOTfzrrSqjr P4ZXLrGRWPNy/sw/ALIXqTzW88PGNxo+ctTObewCpx5JQW37M3KEzGTi15eafz1mCr6+ 23bNii2q4tRuluJppLFuoT61ylCuJsLq6eAuA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=jI83R1A9WgWddgJmmrIJIxnOZusZeXJDWObnpkXgy22PUxDoQg8Eas+LjnOMR42uod 9pPeJqMne+WRzW8rOT+hJOkXGbz1JI+ohUDEJWVtRO84NXcLaM556ULdMrRVTG741Fdt B4bQa5tE4VMC4adrsZc/NLyWsTEL6JwqGMr7o= MIME-Version: 1.0 In-Reply-To: References: Date: Thu, 16 Jun 2011 15:25:32 +0200 Subject: Re: Composing regex from a list From: Vlastimil Brom To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 41 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308230734 news.xs4all.nl 49045 [::ffff:82.94.164.166]:58357 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7741 2011/6/16 TheSaint : > Hello, > Is it possible to compile a regex by supplying a list? > > lst= ['good', 'brilliant'. 'solid'] > re.compile(r'^'(any_of_lst)) > > without to go into a *for* cicle? > In simple cases, you can just join the list of alternatives on "|" and incorporate it in the pattern - e.g. in non capturing parentheses: (?: ...) cf.: >>> >>> lst= ['good', 'brilliant', 'solid'] >>> import re >>> re.findall(r"^(?:"+"|".join(lst)+")", u"solid sample text; brilliant QWERT") [u'solid'] >>> [using findall just to show the result directly, it is not that usual with starting ^ ...] However, if there can be metacharacters like [ ] | . ? * + ... in the alternative "words", you have to use re.escape(...) on each of these before. Or you can use a newer regex implementation with more features http://pypi.python.org/pypi/regex which was just provisionally enhanced with an option for exactly this usecase: cf. Additional features: Named lists on the above page; in this case: >>> import regex # http://pypi.python.org/pypi/regex >>> regex.findall(r"^\L", u"solid sample text; brilliant QWERT", options=lst) [u'solid'] >>> hth, vbr