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.8k Android Studio : Checkbox for user to select options example
+20.3k Golang : How to get own program name during runtime ?
+11.4k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+9k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+8.9k Golang : Accept any number of function arguments with three dots(...)
+11.7k Golang : Gorilla web tool kit secure cookie example
+8.9k Golang : Sort lines of text example
+5.6k Swift : Get substring with rangeOfString() function example
+5.4k Javascript : How to loop over and parse JSON data?
+12.6k Golang : Exit, terminating or aborting a program
+36.4k Golang : How to split or chunking a file to smaller pieces?