Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'string.': 0.05; 'matches': 0.07; 'subject:skip:s 10': 0.07; 'suppose': 0.07; 'preferable': 0.09; 'subject:How': 0.10; 'believes': 0.16; 'earlier.': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'grouped': 0.16; 'message-id:@cskk.homeip.net': 0.16; 're.compile(': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'simpson': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'example': 0.22; 'import': 0.22; 'portion': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'cheers,': 0.24; 'script': 0.25; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'lines': 0.31; 'serve': 0.31; 'file': 0.32; 'says': 0.33; 'beginning': 0.33; 'test': 0.35; 'i.e.': 0.36; 'received:com.au': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'should': 0.36; 'two': 0.37; 'remote': 0.38; 'checks': 0.38; 'received:211': 0.38; 'to:addr:python-list': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'above,': 0.60; 'expression': 0.60; "you're": 0.61; 'first': 0.61; 'content-disposition:inline': 0.62; 'email addr:gmail.com': 0.63; 'refer': 0.63; 'more': 0.64; 'thomas': 0.65; 'hours': 0.66; 'truth': 0.81; 'one).': 0.84; 'regexp': 0.84; 'error;': 0.91 Date: Thu, 10 Jul 2014 19:50:43 +1000 From: Cameron Simpson To: python-list@python.org Subject: Re: How to write match nth grouped subexpression? MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <53ada36b-e1ce-4487-b9ac-0a162e3da8c3@googlegroups.com> User-Agent: Mutt/1.5.21 (2010-09-15) References: <53ada36b-e1ce-4487-b9ac-0a162e3da8c3@googlegroups.com> X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.1 cv=eojmkOZX c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=o06XNvS4DP4A:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=pGLkceISAAAA:8 a=Yd8pRgNG9gmY0Rtm-CIA:9 a=CjuIK1q_8ugA:10 a=MSl-tDqOz04A:10 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404985853 news.xs4all.nl 2835 [2001:888:2000:d::a6]:48530 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74295 On 10Jul2014 01:57, rxjwg98@gmail.com wrote: >It says that: match checks for a match only at the beginning of the string. >Then, it also says that: \1...\9 Matches nth grouped subexpression. > >I don't know how to write a script to include grouped subexpression in match? A grouped subexpression is just a portion of a regexp marked out. They serve two main purposes: to refer to a portion of the expression as you intend, and to mark a section of the regexp for use by a modifier. Here's an example: Suppose you're reading a file and trying to match lines like: Hours logged: 12 You might use a regexp like this: Hours logged: (\d+) If you match with the expression above, the "\d+" portion will match one or more digits i.e. the "12" in the example line earlier. The "(\d+)" is a grouped subexpression, the first one (and only one). If you write a little test script (untested): import re TESTLINE = "Hours logged: 12" regexp = re.compile( r'Hours logged: (\d+)' ) m = regexp.match(TESTLINE) print "subgroup 1:", m.group(1) that should print "12". Cheers, Cameron Simpson Ignorance is preferable to error; and he is less remote from the truth who believes nothing, than he who believes what is wrong. - Thomas Jefferson