Named capturing groups in go
The simplest way to gain access to any subgroup match in a regexp is by using indexes. This is still my preferred method
of choice for one-off tasks, and I use it daily to perform substitutions on my neovim buffers.
However, when introducing a regular expression into a codebase, I try to use named capture groups instead. They allow
you to access the match by a name, and I think they become especially useful for longer and more complex patterns where
they can help to serve as documentation.
Here is one way of using them in Go:
var re = regexp.MustCompile("This file is (?P<size>[0-9]+)(?P<unit>kb|mb|gb)")func main() { str := "This file is 100kb" match := re.FindSubmatch([]byte(str)) fmt.Println(string(match[re.SubexpIndex("size")])) // 100 fmt.Println(string(match[re.SubexpIndex("unit")])) // kb}
This is a fairly simple pattern, but I still think it is easier to read compared to its unnamed counter part:
var re = regexp.MustCompile("This file is ([0-9]+)(kb|mb|gb)")func main() { str := "This file is 100kb" match := re.FindSubmatch([]byte(str)) fmt.Println(string(match[1])) // 100 fmt.Println(string(match[2])) // kb}
The end
I usually tweet something when I've finished writing a new post. You can find me on Twitter
by clickingÂ