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
Example of Switch Case with Break in For Loop
How to replace emoji characters in string using regex in Golang?
How to get first and last element of slice in Golang?
Golang Get current Date and Time in EST, UTC and MST?
Sierpinski triangle in Go Programming Language
How to read current directory using Readdir?