How to write match nth grouped subexpression?

Cameron Simpson cs at zip.com.au
Thu Jul 10 05:50:43 EDT 2014


On 10Jul2014 01:57, rxjwg98 at gmail.com <rxjwg98 at 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 at 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



More information about the Python-list mailing list