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
Regular Expression to get a string between parentheses in Golang
Regular expression to extract DNS host-name or IP Address from string
Regular expression for matching HH:MM time format in Golang
Regular expression to extract all Non-Alphanumeric Characters from a String
Regular expression to split string on white spaces
Regular expression to extract numbers from a string in Golang
Regex to extract image name from HTML in Golang
Regular expression to validate phone number
How to extract text from between html tag using Regular Expressions in Golang?
How to remove symbols from a string in Golang?