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


Groups > comp.lang.python > #74295

Re: How to write match nth grouped subexpression?

Date 2014-07-10 19:50 +1000
From Cameron Simpson <cs@zip.com.au>
Subject Re: How to write match nth grouped subexpression?
References <53ada36b-e1ce-4487-b9ac-0a162e3da8c3@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.11721.1404985853.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 10Jul2014 01:57, rxjwg98@gmail.com <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 <cs@zip.com.au>

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

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


Thread

How to write match nth grouped subexpression? rxjwg98@gmail.com - 2014-07-10 01:57 -0700
  Re: How to write match nth grouped subexpression? Cameron Simpson <cs@zip.com.au> - 2014-07-10 19:50 +1000

csiph-web