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: 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>': 0.22; 'cc:addr:python.org': 0.22; 'stick': 0.24; 'cc:2**0': 0.24; '>': 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: <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 To: Alain Ketterlin Content-Type: multipart/alternative; boundary=14dae94edc77bf4f4004d90de91f Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --14dae94edc77bf4f4004d90de91f Content-Type: text/plain; charset=UTF-8 On Friday, 29 March 2013, Alain Ketterlin wrote: > Victor Hooi > 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 ( '(?Ppattern)'). Then you can do if m.group('case1'): ... elif m.group('case2'): ... -- Arnaud --14dae94edc77bf4f4004d90de91f Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Friday, 29 March 2013, Alain Ketterlin wrote:
Victor Hooi <victorhooi@gmail.com> w= rites:

> =C2=A0 =C2=A0 expression1 =3D re.compile(r'....')
> =C2=A0 =C2=A0 expression2 =3D 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 '...|...'
=C2=A0
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 ca= n name these alternatives ( '(?P<name>pattern)')= . =C2=A0Then you can do

if m.group('case1'= ):
=C2=A0 =C2=A0 ...
elif m.group('case2'):
= =C2=A0 =C2=A0...

--=C2=A0
Arnaud

--14dae94edc77bf4f4004d90de91f--