问题
I am trying to get everything within the outside brackets of the following sql statement in golang regular expressions.
Categories
(// = outside bracket
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)//=outside bracket
how would I use regex to only identify the outer brackets and return everything in between the outside brackets?
回答1:
All you need is to find the first (, then match any characters up to the last ) with
`(?s)\((.*)\)`
Details:
(?s)- allow.to match any character, including a newline\(- a literal(char(.*)- Submatch 1 capturing any zero or more characters\)- a literal)symbol.
See the Go demo:
package main
import (
"fmt"
"regexp"
)
func main() {
s := `Categories
(// = outside bracket
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)//=outside bracket`
re := regexp.MustCompile(`(?s)\((.*)\)`)
m := re.FindAllStringSubmatch(s,-1)
fmt.Printf("Capture value: %s", m[0][1])
}
来源:https://stackoverflow.com/questions/39488210/how-to-get-everything-within-brackets-in-golang-with-regex