Reputation: 733
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?
Upvotes: 3
Views: 6271
Reputation: 626747
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])
}
Upvotes: 4