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


Groups > comp.lang.python > #14420

Re: Question: Optional Regular Expression Grouping

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'oct': 0.02; 'preferably': 0.03; 'escape': 0.04; 'mrab': 0.04; 'string.': 0.04; 'prefer.': 0.09; 'backslashes': 0.16; 'literal.': 0.16; 'op:': 0.16; 'patterns,': 0.16; 'repetition': 0.16; 'mon,': 0.16; 'wrote:': 0.16; 'subject:Question': 0.21; "doesn't": 0.22; 'header:In-Reply- To:1': 0.22; 'pm,': 0.24; 'starts': 0.24; 'string': 0.26; 'problem': 0.28; 'correct': 0.28; 'matches': 0.29; 'message- id:@mail.gmail.com': 0.29; 'match': 0.30; 'module': 0.30; 'construct': 0.30; 'pattern': 0.30; "skip:' 10": 0.30; 'actually': 0.33; 'to:addr:python-list': 0.33; 'instead': 0.33; 'rather': 0.35; 'notes': 0.35; 'core': 0.36; 'using': 0.37; 'but': 0.37; 'think': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'characters': 0.39; 'either': 0.39; 'to:addr:python.org': 0.39; "i'd": 0.40; 'raw': 0.40; 'received:209.85.215.174': 0.67; 'received:mail- ey0-f174.google.com': 0.67; "'foo'": 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=taVeJCR6M5mEqRVLKFtouuAZuVcysc+wCAJch2cuAhk=; b=SnaSeyBPTBb8k+oEJgNEuIiOZ3iM4NdDotvbXsR4keYSywoHL5WzXJ3bx/qesDZn87 hQCA0yW8xuTmruEWR4b+MUmbSsz9V8SmKj9XdeoBDHFUDMbMllMpYud4KFcdFPGZ/ird m2RChAjAaMn54jv7pvN9FL3Y08OKDQ7h4y1Sc=
MIME-Version 1.0
In-Reply-To <4E937669.9060308@mrabarnett.plus.com>
References <8537a563-69d7-4bce-b192-d9427aad0a7c@i14g2000yqg.googlegroups.com> <4E937669.9060308@mrabarnett.plus.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Mon, 10 Oct 2011 17:03:02 -0600
Subject Re: Question: Optional Regular Expression Grouping
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.1874.1318287813.27778.python-list@python.org> (permalink)
Lines 18
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1318287813 news.xs4all.nl 2472 [2001:888:2000:d::a6]:40413
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:14420

Show key headers only | 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