Golang : Use regular expression to validate domain name
There are times when a user enter an invalid domain name as input and it is good to validate the domain name before processing further or store into database. So far, the best way to validate a domain name in Golang is still via regular expression.
There are couple of regular expressions out there that are generally accepted regular expression to validate a domain name. However, for this tutorial we will use :
^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|
([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9])).([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}.[a-zA-Z]{2,3})$
Here we go!
package main
import (
"fmt"
"regexp"
)
func validateDomainName(domain string) bool {
// Golang does not support Perl syntax ((?
// will throw out :
// error parsing regexp: invalid or unsupported Perl syntax: `(?!`
//patternStr := "^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+[A-Za-z]{2,6}$"
// use regular expression without Perl syntax
RegExp := regexp.MustCompile(`^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z
]{2,3})$`)
return RegExp.MatchString(domain)
}
func main() {
domName := "www.golang.org"
if !validateDomainName(domName) {
fmt.Printf("Domain Name %s is invalid\n", domName)
} else {
fmt.Printf("Domain Name %s is VALID\n", domName)
}
domName = "www.socketloop,.com"
if !validateDomainName(domName) {
fmt.Printf("Domain Name %s is invalid\n", domName)
} else {
fmt.Printf("Domain Name %s is VALID\n", domName)
}
domName = "subdomain-socketloop.com"
if !validateDomainName(domName) {
fmt.Printf("Domain Name %s is invalid\n", domName)
} else {
fmt.Printf("Domain Name %s is VALID\n", domName)
}
domName = "-socketloop.com" // invalid starts with hyphen
if !validateDomainName(domName) {
fmt.Printf("Domain Name %s is invalid\n", domName)
} else {
fmt.Printf("Domain Name %s is VALID\n", domName)
}
domName = "socketloop.co_" // invalid ends with underscore
if !validateDomainName(domName) {
fmt.Printf("Domain Name %s is invalid\n", domName)
} else {
fmt.Printf("Domain Name %s is VALID\n", domName)
}
domName = "subdomain.socketloop.com"
if !validateDomainName(domName) {
fmt.Printf("Domain Name %s is invalid\n", domName)
} else {
fmt.Printf("Domain Name %s is VALID\n", domName)
}
}
Output :
Domain Name www.golang.org is VALID
Domain Name www.socketloop,.com is invalid
Domain Name subdomain-socketloop.com is VALID
Domain Name -socketloop.com is invalid
Domain Name socketloop.co_ is invalid
Domain Name subdomain.socketloop.com is VALID
References :
https://www.debuggex.com/r/y4Xe_hDVO11bv1DV
http://stackoverflow.com/questions/10306690/domain-name-validation-with-regex
See also : Golang : Validate email address with regular expression
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+24.4k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+14.2k Android Studio : Use image as AlertDialog title with custom layout example
+6.3k Golang : Break string into a slice of characters example
+6.7k Mac/Linux/Windows : Get CPU information from command line
+19.3k Golang : How to count the number of repeated characters in a string?
+4.6k MariaDB/MySQL : How to get version information
+7.7k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+14.4k Golang : Send email with attachment(RFC2822) using Gmail API example
+9.2k Golang : How to get garbage collection data?
+12.2k Golang : Encrypt and decrypt data with x509 crypto
+11.2k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+10.5k Golang : Simple File Server