Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #74293 > unrolled thread
| Started by | rxjwg98@gmail.com |
|---|---|
| First post | 2014-07-10 01:57 -0700 |
| Last post | 2014-07-10 19:50 +1000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
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
| From | rxjwg98@gmail.com |
|---|---|
| Date | 2014-07-10 01:57 -0700 |
| Subject | How to write match nth grouped subexpression? |
| Message-ID | <53ada36b-e1ce-4487-b9ac-0a162e3da8c3@googlegroups.com> |
Hi, 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? Thanks,
[toc] | [next] | [standalone]
| From | Cameron Simpson <cs@zip.com.au> |
|---|---|
| Date | 2014-07-10 19:50 +1000 |
| Message-ID | <mailman.11721.1404985853.18130.python-list@python.org> |
| In reply to | #74293 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web