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
How to get struct variable information using reflect package?
How pointer & and * and ** works in Golang?
How to print string with double quote in Go?
Pass different types of arguments in variadic function
How to add Watermark or Merge two image?
How to concatenate two or more slices in Golang?
Copy an array by value and reference into another array
Regular expression to validate email address
How to Unmarshal nested JSON structure?
How to Convert string to float type in Go?