Regular Expression to get a string between parentheses in Golang
Example
package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
str1 := "This is a (sample) ((string)) with (SOME) special words"
re := regexp.MustCompile(`\((.*?)\)`)
fmt.Printf("Pattern: %v\n", re.String()) // print pattern
fmt.Println("\nText between parentheses:")
submatchall := re.FindAllString(str1, -1)
for _, element := range submatchall {
element = strings.Trim(element, "(")
element = strings.Trim(element, ")")
fmt.Println(element)
}
}
Output
Pattern: \((.*?)\)
Text between parentheses:
sample
string
SOME
Most Helpful This Week
How to get Dimensions of an image type jpg jpeg png or gif ?
How to fetch an Integer variable as String in Go?
Normal function parameter with variadic function parameter
How to print struct variables data in Golang?
How to create Empty and Nil Slice?
How to reads and decodes JSON values from an input stream?