How to check if a string contains a numbers in Golang?
A string contains a number if any of the characters are digits (0-9).
Using Regex to check if a string contains digit
package main
import (
"fmt"
"regexp"
)
func main() {
word := "test25"
numeric := regexp.MustCompile(`\d`).MatchString(word)
fmt.Println(numeric)
}
Regular expression is widely used for pattern matching. The regexp package provides support for regular expressions, which allow complex patterns to be found in strings. The regexp.MustCompile() function is used to create the regular expression and the MatchString() function returns a bool that indicates whether a pattern is matched by the string.
Most Helpful This Week
Simple example of Map initialization in Go
Get Year, Month, Day, Hour, Min and Second from a specified date
How to iterate over an Array using for loop?
Example: How to use TeeReader from IO Package in Golang?
Golang Slice vs Map Benchmark Testing
How can I convert a string variable into Boolean, Integer or Float type in Golang?
How to get struct variable information using reflect package?
Example to create custom error
How to get Dimensions of an image type jpg jpeg png or gif ?
Split a string at uppercase letters using regular expression in Golang