regexp docs for 'Index' ?

65 views
Skip to first unread message

Peter Galbavy

unread,
Dec 14, 2023, 3:38:57 AM12/14/23
to golang-nuts
I noticed today that the regexp docs read:

If 'Index' is present, matches and submatches are identified by byte index pairs within the input string: result[2*n:2*n+2] identifies the indexes of the nth submatch. 

I think that should be result[2*n:2*n+1] - at least in my code that's how it is working.

If I'm right, happy to raise a doc bug, but never done that, so not sure how - for a one digit change?

Peter

Brian Candler

unread,
Dec 14, 2023, 4:06:00 AM12/14/23
to golang-nuts
[a:b] gives you the elements from a to b-1 inclusive. So if you want the pair at position x, it has to be [x:x+2]

Peter Galbavy

unread,
Dec 14, 2023, 5:30:06 AM12/14/23
to golang-nuts
Ah! My bad. I misread the docs - "result" is the input, while the "n" indexes are actually the values of the submatches, not the indexes. I still think it's confusing, but makes more sense when I write it like this: https://go.dev/play/p/SHl_aSU3kPZ 

Brian Candler

unread,
Dec 14, 2023, 8:38:35 AM12/14/23
to golang-nuts
No: result is the result, which is a bunch of 2-element arrays concatenated.

As the documentation says: "result[2*n:2*n+2] identifies the indexes of the nth submatch"  (i.e. the start index and the end index)

In principle, accessing the i'th result is like this:

start, end := matches[i*2:i*2+2]

However Go doesn't actually allow a slice to be unpacked that way (only certain multi-valued things like function results and channel receives)
Reply all
Reply to author
Forward
0 new messages