Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 234
Plastikfan Avatar asked May 27 '15 13:05

Plastikfan


People also ask

How do Capturing groups work in regex?

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d" "o" and "g" .

How do I reference a capture group in regex?

If your regular expression has named capturing groups, then you should use named backreferences to them in the replacement text. The regex (?' name'group) has one group called “name”. You can reference this group with ${name} in the JGsoft applications, Delphi, .

How do you capture a group in go?

For a group of stones to be captured, all the intersections touching the group must be filled. The group must not only be surrounded on the outside, but empty spaces inside the group adjacent to the stones must be filled also. A single empty space inside a group is called an eye.

What is capturing group in regex Javascript?

A part of a pattern can be enclosed in parentheses (...) . This is called a “capturing group”. That has two effects: It allows to get a part of the match as a separate item in the result array.


1 Answers

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()) } 
like image 165
thwd Avatar answered Sep 20 '22 20:09

thwd