Positions of regexp groups

  • Thread starter Magnus Lie Hetland
  • Start date
M

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...?
 
J

John Machin

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?
 
T

Tim Peters

[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.
 
M

Magnus Lie Hetland

[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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top