Regular expression to extract domain from URL
Example
package main
import (
"fmt"
"regexp"
)
func main() {
str1 := `http://www.suon.co.uk/product/1/7/3/`
re := regexp.MustCompile(`^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/\n]+)`)
fmt.Printf("Pattern: %v\n", re.String()) // print pattern
fmt.Println(re.MatchString(str1)) // true
submatchall := re.FindAllString(str1,-1)
for _, element := range submatchall {
fmt.Println(element)
}
}
Output
Pattern: ^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/\n]+)
true
http://www.suon.co.uk
Most Helpful This Week
Passing multiple string arguments to a variadic function
How to check if a string contains a white space in Golang?
Example to compare Println vs Printf
Simple function with return value in Golang
Regular expression to extract text between square brackets
How to check if a string contains only letters in Golang?