Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'continuation': 0.07; 'forum,': 0.09; 'from:addr:python': 0.09; '"="': 0.16; 'collapsed': 0.16; 'constructed': 0.16; 'expression,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'repetition': 0.16; 'reply-to:addr:python-list': 0.16; 'skip:r 50': 0.16; 'suggestions?': 0.16; 'looked': 0.16; 'this:': 0.16; 'wrote:': 0.16; "haven't": 0.20; 'figure': 0.21; 'resolution': 0.21; 'subject:Question': 0.21; 'header:In-Reply-To:1': 0.22; "i'm": 0.27; 'received:84': 0.28; 'problem': 0.28; 'import': 0.28; 'print': 0.29; '(maybe': 0.30; "didn't": 0.31; 'hi,': 0.32; 'does': 0.32; 'anyone': 0.32; "what's": 0.33; "can't": 0.33; 'to:addr:python-list': 0.33; 'instead': 0.33; "i've": 0.34; 'however,': 0.34; 'header:User-Agent:1': 0.34; 'reply- to:addr:python.org': 0.34; 'skip:" 20': 0.35; 'regular': 0.35; 'file': 0.36; 'skip:" 10': 0.36; 'using': 0.37; 'but': 0.37; 'something': 0.37; 'two': 0.37; 'subject:: ': 0.39; 'correctly': 0.39; 'enough': 0.39; 'missing': 0.39; 'to:addr:python.org': 0.39; 'case': 0.39; "i'd": 0.40; 'third': 0.40; 'below': 0.62; 'believe': 0.65; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.71; 'demo': 0.80; 'subject:: ': 0.91 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ap8GAJp1k07Unw4S/2dsb2JhbABDmUCMZYF/gQWBUwEBBThAEQsIEAkWDwkDAgECAQ04EwgBAYd8uVKHQwSMGk6MOIwq Date: Mon, 10 Oct 2011 23:49:13 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Question: Optional Regular Expression Grouping References: <8537a563-69d7-4bce-b192-d9427aad0a7c@i14g2000yqg.googlegroups.com> In-Reply-To: <8537a563-69d7-4bce-b192-d9427aad0a7c@i14g2000yqg.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1318287022 news.xs4all.nl 2403 [2001:888:2000:d::a6]:35252 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:14418 On 10/10/2011 22:57, galyle wrote: > 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) Instead of "\S" I'd recommend using "[^\]]", or using a lazy repetition "\S+?". You'll also need to handle the space before the "=" in line3. valid_line = re.compile(r'^\[(\[^\]]+)\]\[(\[^\]]+)\](?:\s+|\[(\[^\]]+)\])\s*=|\s+[\d\[\']+.*$')