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
+46k Golang : Encode image to base64 example
+13.2k Golang : Get constant name from value
+10.3k Golang : Select region of interest with mouse click and crop from image
+35.6k Golang : Save image to PNG, JPEG or GIF format.
+9.5k Golang : interface - when and where to use examples
+30.7k Golang : Calculate percentage change of two values
+7.5k Golang : Lock executable to a specific machine with unique hash of the machine
+10.2k Swift : Convert (cast) String to Integer
+4.4k Mac OSX : Get disk partitions' size, type and name
+6.6k Swift : substringWithRange() function example
+5.5k Unix/Linux : Get reboot history or check when was the last reboot date
+8.6k Golang : Take screen shot of browser with JQuery example