Golang : Regular Expression for alphanumeric and underscore
Problem :
Need a regular expression that only allows upper, lowercase characters, underscores and numbers for Go.
Solution :
Use this regular expression
"^[a-zA-Z0-9_]*$"
Explanations:
^ : start of string
[ : beginning of character group
a-z : any lowercase letter
A-Z : any uppercase letter
0-9 : any digit
_ : underscore
] : end of character group
* : zero or more of the given characters
$ : end of string
Go source code :
package main
import "fmt"
import "regexp"
func main() {
a := "testing_123"
re := regexp.MustCompile("^[a-zA-Z0-9_]*$")
fmt.Println(re.MatchString("123"))
fmt.Println(re.MatchString("abc"))
fmt.Println(re.MatchString(a))
fmt.Println(re.MatchString("世界"))
}
Output :
true
true
true
false
Reference :
http://stackoverflow.com/questions/336210/regular-expression-for-alphanumeric-and-underscores
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
+21.9k Golang : Fix type interface{} has no field or no methods and type assertions example
+8.8k Golang : Setting variable value with ldflags
+27.7k Golang : Save map/struct to JSON or XML file
+16.6k Golang : Get command line arguments
+19.7k Golang : Get password from console input without echo or masked
+31.5k Delete a directory in Go
+22.7k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+24.7k Golang : Convert(cast) string to uint8 type and back to string
+40.6k Golang : Get hardware information such as disk, memory and CPU usage
+9.8k Golang : Characters limiter example
+17.2k Golang : Example for RSA package functions
+9.4k Golang : Simple File Server