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


Groups > comp.lang.python > #14420

Re: Question: Optional Regular Expression Grouping

References <8537a563-69d7-4bce-b192-d9427aad0a7c@i14g2000yqg.googlegroups.com> <4E937669.9060308@mrabarnett.plus.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-10-10 17:03 -0600
Subject Re: Question: Optional Regular Expression Grouping
Newsgroups comp.lang.python
Message-ID <mailman.1874.1318287813.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Oct 10, 2011 at 4:49 PM, MRAB <python@mrabarnett.plus.com> wrote:
> Instead of "\S" I'd recommend using "[^\]]", or using a lazy repetition
> "\S+?".

Preferably the former.  The core problem is that the regex matches
ambiguously on the problem string.  Lazy repetition doesn't remove
that ambiguity; it merely attempts to make the module prefer the match
that you prefer.

Other notes to the OP:  Always use raw strings (r'') when writing
regex patterns, to make sure the backslashes are escape characters in
the pattern rather than in the string literal.

The '^foo|bar$' construct you're using is wonky.  I think you're
writing this to mean "match if the entire string is either 'foo' or
'bar'".  But what that actually matches is "anything that either
starts with 'foo' or ends with 'bar'".  The correct way to do the
former would be either '^foo$|^bar$' or '^(?:foo|bar)$'.

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


Thread

Question:  Optional Regular Expression Grouping galyle <galyle@gmail.com> - 2011-10-10 14:57 -0700
  Re: Question:  Optional Regular Expression Grouping MRAB <python@mrabarnett.plus.com> - 2011-10-10 23:49 +0100
  Re: Question: Optional Regular Expression Grouping Vlastimil Brom <vlastimil.brom@gmail.com> - 2011-10-11 00:59 +0200
    Re: Question: Optional Regular Expression Grouping galyle <galyle@gmail.com> - 2011-10-10 16:24 -0700
  Re: Question: Optional Regular Expression Grouping Ian Kelly <ian.g.kelly@gmail.com> - 2011-10-10 17:03 -0600

csiph-web