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


Groups > comp.lang.python > #7741

Re: Composing regex from a list

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 <vlastimil.brom@gmail.com>
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 <itcu3f$b1k$1@speranza.aioe.org>
References <itcu3f$b1k$1@speranza.aioe.org>
Date Thu, 16 Jun 2011 15:25:32 +0200
Subject Re: Composing regex from a list
From Vlastimil Brom <vlastimil.brom@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15.1308230734.1164.python-list@python.org> (permalink)
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

Show key headers only | View raw


2011/6/16 TheSaint <nobody@nowhere.net.no>:
> 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<options>", u"solid sample text; brilliant QWERT", options=lst)
[u'solid']
>>>

hth,
  vbr

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


Thread

Composing regex from a list TheSaint <nobody@nowhere.net.no> - 2011-06-16 20:48 +0800
  Re: Composing regex from a list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-16 13:09 +0000
    Re: Composing regex from a list TheSaint <nobody@nowhere.net.no> - 2011-06-17 19:45 +0800
  Re: Composing regex from a list Vlastimil Brom <vlastimil.brom@gmail.com> - 2011-06-16 15:25 +0200

csiph-web