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 read input from console line?
Example: How to use ReadAtLeast from IO Package in Golang?
How to iterate over an Array using for loop?
Example to create custom error
How to get first and last element of slice in Golang?
How to Unmarshal nested JSON structure?
How to rotate an image?
Different ways for Integer to String Conversions
Select single argument from all arguments of variadic function
Convert specific UTC date time to PST, HST, MST and SGT