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
+10.4k Golang : Force download file example
+9.9k Golang : Accurate and reliable decimal calculations
+7.2k Golang : How to protect your source code from client, hosting company or hacker?
+14k Golang : Logging with logrus
+5.5k Golang : get the current working directory of a running program
+8.2k Golang : Forwarding a local port to a remote server example
+29.1k Golang : Upload and download file to/from AWS S3
+11.3k Golang : Delete files by extension
+4.9k Golang : Array mapping with Interface
+12.8k Golang : Get sub string example
+31.4k Golang : convert(cast) bytes to string
+3.2k Golang *File points to a file or directory ?