regexgocapture-group

How to get capturing group functionality in Go regular expressions


I'm porting a library from Ruby to Go, and have just discovered that regular expressions in Ruby are not compatible with Go (google RE2). It's come to my attention that Ruby & Java (plus other languages use PCRE regular expressions (perl compatible, which supports capturing groups)), so I need to re-write my expressions so that they compile ok in Go.

For example, I have the following regex:

`(?<Year>\d{4})-(?<Month>\d{2})-(?<Day>\d{2})`

This should accept input such as:

2001-01-20

The capturing groups allow the year, month and day to be captured into variables. To get the value of each group, it's very easy; you just index into the returned matched data with the group name and you get the value back. So, for example to get the year, something like this pseudo code:

m=expression.Match("2001-01-20")
year = m["Year"]

This is a pattern I use a lot in my expressions, so I have a lot of re-writing to do.

So, is there a way to get this kind of functionality in Go regexp; how should I re-write these expressions?


Solution

  • how should I re-write these expressions?

    Add some Ps, as defined here:

    (?P<Year>\d{4})-(?P<Month>\d{2})-(?P<Day>\d{2})
    

    Cross reference capture group names with re.SubexpNames().

    And use as follows:

    package main
    
    import (
        "fmt"
        "regexp"
    )
    
    func main() {
        r := regexp.MustCompile(`(?P<Year>\d{4})-(?P<Month>\d{2})-(?P<Day>\d{2})`)
        fmt.Printf("%#v\n", r.FindStringSubmatch(`2015-05-27`))
        fmt.Printf("%#v\n", r.SubexpNames())
    }