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


Groups > comp.lang.python > #42234

Re: Doing both regex match and assignment within a If loop?

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <arnodel@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'elif': 0.05; 'expressions': 0.07; 'subject:both': 0.07; 'alain': 0.09; 'alternatives': 0.09; 'friday,': 0.09; 'cc:addr:python-list': 0.11; 'wrote:': 0.18; 'email addr:gmail.com&gt;': 0.22; 'cc:addr:python.org': 0.22; 'stick': 0.24; 'cc:2**0': 0.24; '&gt;': 0.26; 'recognized': 0.26; 'least': 0.26; 'header:In-Reply- To:1': 0.27; 'returned': 0.30; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; 'writes:': 0.31; 'regular': 0.32; 'could': 0.34; 'received:google.com': 0.35; 'representing': 0.36; 'subject:?': 0.36; 'sure': 0.39; 'march': 0.61; 'name': 0.63; 'victor': 0.84; '2013,': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=rTHRh5LEKUYGatgcmJDTl5m/ZzS9o6RAejDgawmPeGk=; b=0VU7E2QB6dNO8z8rPofJZNJv5Xn45VPG0/Qy7jBvMoK8egL3yXCeVV9IhmBB+6AQLF NmywiNe30+Unt3OP3iYojr6JD123cNi9ojWT2T8lSB4kTgPxfg9VlrjoMi6wt53vgfxb ekyc3dobc8D+lBAycV/XVo7AS/Oqscnfp7GbFplyI5hGaPSyjNjBkzQOAxWcYAOdos+w gFVPFH2CbYufP3nW0A77m0O6Vwwvp74VOtswGrSj38FDN0qZagQJMaXOWazmgWZ0Xq/A uqCiMZarUHB82IJjiBf835fuBkCkw70rrql52JZEOMW464+N4pFKaIrcZz/iXOcUR4z2 kktA==
MIME-Version 1.0
X-Received by 10.112.3.198 with SMTP id e6mr1137633lbe.118.1364553686272; Fri, 29 Mar 2013 03:41:26 -0700 (PDT)
In-Reply-To <87txnuv7tl.fsf@dpt-info.u-strasbg.fr>
References <f8598e49-67af-4a97-98db-9fd69d0182ae@googlegroups.com> <87txnuv7tl.fsf@dpt-info.u-strasbg.fr>
Date Fri, 29 Mar 2013 10:41:26 +0000
Subject Re: Doing both regex match and assignment within a If loop?
From Arnaud Delobelle <arnodel@gmail.com>
To Alain Ketterlin <alain@dpt-info.u-strasbg.fr>
Content-Type multipart/alternative; boundary=14dae94edc77bf4f4004d90de91f
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.3943.1364553689.2939.python-list@python.org> (permalink)
Lines 62
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364553689 news.xs4all.nl 6987 [2001:888:2000:d::a6]:35741
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42234

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Friday, 29 March 2013, Alain Ketterlin wrote:

> Victor Hooi <victorhooi@gmail.com <javascript:;>> writes:
>
> >     expression1 = re.compile(r'....')
> >     expression2 = re.compile(r'....')
> [...]
>
> Just a quick remark: regular expressions are pretty powerful at
> representing alternatives. You could just stick everything inside a
> single re, as in '...|...'
>
>
Then use the returned match to check which alternative was recognized
> (make sure you have at least one group in each alternative).
>
>
Yes, and for extra ease/clarity you can name these alternatives (
'(?P<name>pattern)').  Then you can do

if m.group('case1'):
    ...
elif m.group('case2'):
   ...

-- 
Arnaud

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


Thread

Doing both regex match and assignment within a If loop? Victor Hooi <victorhooi@gmail.com> - 2013-03-28 21:00 -0700
  Re: Doing both regex match and assignment within a If loop? Chris Rebert <clp2@rebertia.com> - 2013-03-28 21:29 -0700
  Re: Doing both regex match and assignment within a If loop? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-29 06:45 +0000
  Re: Doing both regex match and assignment within a If loop? Peter Otten <__peter__@web.de> - 2013-03-29 09:27 +0100
  Re: Doing both regex match and assignment within a If loop? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2013-03-29 11:06 +0100
    Re: Doing both regex match and assignment within a If loop? Arnaud Delobelle <arnodel@gmail.com> - 2013-03-29 10:41 +0000
    Re: Doing both regex match and assignment within a If loop? Neil Cerutti <neilc@norwich.edu> - 2013-03-29 12:51 +0000
  Re: Doing both regex match and assignment within a If loop? Mitya Sirenef <msirenef@lightbird.net> - 2013-03-29 09:49 -0400

csiph-web