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)

On Thursday 14 December 2023 at 10:30:06 UTC Peter Galbavy wrote:

> 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 
>
> On Thursday 14 December 2023 at 09:06:00 UTC Brian Candler wrote:
>
>> [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]
>>
>> https://go.dev/play/p/3nvEfOjdfnj
>>
>> On Thursday 14 December 2023 at 08:38:57 UTC Peter Galbavy wrote:
>>
>>> 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
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/f19eff98-2073-4229-a631-7856dd0952efn%40googlegroups.com.

Reply via email to