Positions of regexp groups

2 views
Skip to first unread message

Magnus Lie Hetland

unread,
Apr 8, 2005, 9:18:27 AM4/8/05
to
Just a quick question: Does anyone have a simple way of finding the
positions (start, end) of the groups in a regexp match? AFAICS, the re
API can only return the contents...?

--
Magnus Lie Hetland Fall seven times, stand up eight
http://hetland.org [Japanese proverb]

John Machin

unread,
Apr 8, 2005, 9:48:27 AM4/8/05
to
On Fri, 8 Apr 2005 13:18:27 +0000 (UTC), m...@selje.idi.ntnu.no (Magnus
Lie Hetland) wrote:

>Just a quick question: Does anyone have a simple way of finding the
>positions (start, end) of the groups in a regexp match? AFAICS, the re
>API can only return the contents...?

These documented methods of the match object aren't simple enough???
"""
start( [group])
end( [group])

Return the indices of the start and end of the substring matched by
group
"""


and there's span() which returns a tuple (start, end) ... what simpler
could you ask for, Mr Occam?


Tim Peters

unread,
Apr 8, 2005, 9:49:30 AM4/8/05
to pytho...@python.org
[Magnus Lie Hetland]

> Just a quick question: Does anyone have a simple way of finding the
> positions (start, end) of the groups in a regexp match? AFAICS, the re
> API can only return the contents...?

Read the docs for match objects, esp. the start(), end(), and span() methods.

Magnus Lie Hetland

unread,
Apr 10, 2005, 8:44:45 AM4/10/05
to
In article <mailman.1535.1112968...@python.org>, Tim

Right. I was just now editing the regex-stuff in my book for the
second edition, and found that I had, in fact, written about just this
behaviour. <blush/>

Oh, well. I guess it's a feature I don't use much :}

Thanks, anyway.

Reply all
Reply to author
Forward
0 new messages