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