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?

References <f8598e49-67af-4a97-98db-9fd69d0182ae@googlegroups.com> <87txnuv7tl.fsf@dpt-info.u-strasbg.fr>
Date 2013-03-29 10:41 +0000
Subject Re: Doing both regex match and assignment within a If loop?
From Arnaud Delobelle <arnodel@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3943.1364553689.2939.python-list@python.org> (permalink)

Show all headers | 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