FAQ
Hi,

I'm currently working with FindAllStringSubmatchIndex
<http://golang.org/pkg/regexp/#Regexp.FindAllStringSubmatchIndex> of the
regexp
package and I'm facing a problem when trying to use it with
overlapping submatches. You can see and run my minimal example
here <http://play.golang.org/p/3w62OXUvWx>. The code is the following:

package main

import "fmt"
import "regexp"

func main() {
     str := "abc"

     re := regexp.MustCompile("(ab)|(bc)")
     fmt.Println(re.FindAllStringSubmatchIndex(str, -1))
     // Prints [[0 2 0 2 -1 -1]]
     // but I was expecting [[0 2 0 2 1 3]]
     // or [[1 3 0 2 1 3]]

     re = regexp.MustCompile("(bc)|(ab)")
     fmt.Println(re.FindAllStringSubmatchIndex(str, -1))
     // Prints [[0 2 -1 -1 0 2]]
}

Here, the two submatches of the regexp individually match the string.
However, the return value of FindAllStringSubmatchIndex indicates
that only the first submatch, according to the matching order, has
matched. I understand the ambiguity for the first pair which could be
both [0 2] and [1 3] but I don't why the (bc) term is simply ignored.

Is there a reason behind that missed match and more importantly, is
there a simple way to obtain the expected result i.e. knowing if each
submatch matches, even in case of overlapping?

Thank you in advance.

Best regards,

--
Anthony Seure

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Nigel Tao at Mar 20, 2015 at 5:55 am

    On Fri, Mar 20, 2015 at 11:20 AM, wrote:
    Is there a reason behind that missed match and more importantly, is
    there a simple way to obtain the expected result i.e. knowing if each
    submatch matches, even in case of overlapping?
    I'm guessing that the reason is efficiency. In any case,
    http://golang.org/pkg/regexp/ explicitly says "non-overlapping" in "If
    'All' is present, the routine matches successive non-overlapping
    matches".

    If you want overlapping matches, I'd run two separate regexps and
    merge the results.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 20, '15 at 4:46a
activeMar 20, '15 at 5:55a
posts2
users2
websitegolang.org

2 users in discussion

Anthony Seure: 1 post Nigel Tao: 1 post

People

Translate

site design / logo © 2021 Grokbase