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


Groups > comp.lang.python > #14419

Re: Question: Optional Regular Expression Grouping

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <vlastimil.brom@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.012
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'continuation': 0.07; 'default:': 0.09; 'forum,': 0.09; 'collapsed': 0.16; 'constructed': 0.16; 'excluding': 0.16; 'explicitely': 0.16; 'expression,': 0.16; 'skip:r 50': 0.16; 'suggestions?': 0.16; 'cc:addr:python-list': 0.16; 'looked': 0.16; 'this:': 0.16; 'cc:no real name:2**0': 0.20; "haven't": 0.20; 'figure': 0.21; 'resolution': 0.21; 'subject:Question': 0.21; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; '(or': 0.23; "i'm": 0.27; 'received:209.85.220': 0.27; 'problem': 0.28; 'url:mailman': 0.28; 'import': 0.28; 'message-id:@mail.gmail.com': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.30; 'match': 0.30; '(maybe': 0.30; 'pattern': 0.30; "didn't": 0.31; 'hi,': 0.32; 'closing': 0.32; 'anyone': 0.32; "what's": 0.33; "can't": 0.33; 'url:listinfo': 0.33; "i've": 0.34; 'however,': 0.34; 'causing': 0.34; 'like:': 0.34; 'skip:" 20': 0.35; 'regular': 0.35; 'file': 0.36; 'url:python': 0.36; 'problems': 0.36; 'skip:" 10': 0.36; 'opposed': 0.37; 'but': 0.37; 'something': 0.37; 'two': 0.37; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'non': 0.38; 'subject:: ': 0.39; 'correctly': 0.39; 'enough': 0.39; 'missing': 0.39; 'case': 0.39; "i'd": 0.40; 'third': 0.40; 'below': 0.62; 'believe': 0.65; 'demo': 0.80; 'skip:r 70': 0.84; 'greedy': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=Dtq9UaN7higFiIbFzQ/GL0K8o8n7mx1HVSEuE/gaPZg=; b=VVCuGC5gL0nU/AckEL3M0xhdu0gZjZMEK2trUkdQ4kcSOxM0N6Ow5MkYZ5JQaHKEa9 U02edOPAx/ymXLpaymDrjmJjc93uly5NheYNIbY1zJdW7J3YYVPwQQWFwsXIpaBMoQ8+ YpFSb1gbXCBKWkSTDXwI8EhSG2maA9pkmIz1M=
MIME-Version 1.0
In-Reply-To <8537a563-69d7-4bce-b192-d9427aad0a7c@i14g2000yqg.googlegroups.com>
References <8537a563-69d7-4bce-b192-d9427aad0a7c@i14g2000yqg.googlegroups.com>
Date Tue, 11 Oct 2011 00:59:46 +0200
Subject Re: Question: Optional Regular Expression Grouping
From Vlastimil Brom <vlastimil.brom@gmail.com>
To galyle <galyle@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
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.1873.1318287589.27778.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1318287589 news.xs4all.nl 2514 [2001:888:2000:d::a6]:60690
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:14419

Show key headers only | View raw


2011/10/10 galyle <galyle@gmail.com>:
> HI, I've looked through this forum, but I haven't been able to find a
> resolution to the problem I'm having (maybe I didn't look hard enough
> -- I have to believe this has come up before).  The problem is this:
> I have a file which has 0, 2, or 3 groups that I'd like to record;
> however, in the case of 3 groups, the third group is correctly
> captured, but the first two groups get collapsed into just one group.
> I'm sure that I'm missing something in the way I've constructed my
> regular expression, but I can't figure out what's wrong.  Does anyone
> have any suggestions?
>
> The demo below showcases the problem I'm having:
>
> import re
>
> valid_line = re.compile('^\[(\S+)\]\[(\S+)\](?:\s+|\[(\S+)\])=|\s+[\d\
> [\']+.*$')
> line1 = "[field1][field2] = blarg"
> line2 = "    'a continuation of blarg'"
> line3 = "[field1][field2][field3] = blorg"
>
> m = valid_line.match(line1)
> print 'Expected: ' + m.group(1) + ', ' + m.group(2)
> m = valid_line.match(line2)
> print 'Expected: ' + str(m.group(1))
> m = valid_line.match(line3)
> print 'Uh-oh: ' + m.group(1) + ', ' + m.group(2)
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Hi,
I believe, the space before = is causing problems (or the pattern missing it);
you also need non greedy quantifiers +? to match as little as possible
as opposed to the greedy default:

valid_line = re.compile('^\[(\S+?)\]\[(\S+?)\](?:\s+|\[(\S+)\])\s*=|\s+[\d\[\']+.*$')

or you can use word-patterns explicitely excluding the closing ], like:

valid_line = re.compile('^\[([^\]]+)\]\[([^\]]+)\](?:\s+|\[([^\]]+)\])\s*=|\s+[\d\[\']+.*$')

hth
 vbr

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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