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
Find element in a slice and move it to first position?
Find length of Channel, Pointer, Slice, String and Map
How to get struct variable information using reflect package?
Get Year, Month, Day, Hour, Min and Second from a specified date
Example of Pointers with Struct
Different ways to convert Byte Array into String