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
+6.6k Golang : Skip or discard items of non-interest when iterating example
+6.3k Golang : Detect face in uploaded photo like GPlus
+5k Swift : Convert (cast) Float to Int or Int32 value
+16.5k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+9.4k Golang : Changing a RGBA image number of channels with OpenCV
+12.6k Android Studio : Highlight ImageButton when pressed on example
+5.1k PHP : See installed compiled-in-modules
+10.5k Golang : Bubble sort example
+16.9k Golang : Get input from keyboard
+22.9k Golang : Calculate time different
+25.1k Golang : Storing cookies in http.CookieJar example
+29.8k Golang : Get time.Duration in year, month, week or day