Is "three" a digit?

Why does the regular expression [0-9]*, which is supposed to match a string with zero or more digits, match a string with characters in it?

matched, err := regexp.MatchString(`[0-9]*`, "12three45")
fmt.Println(matched) // true
fmt.Println(err)     // nil (regexp is valid)


The function regexp.MatchString (as well as most functions in the regexp package) does substring matching.

To check if a full string matches [0-9]*, anchor the start and the end of the regular expression:

  • the caret ^ matches the beginning of a text or line,
  • the dollar sign $ matches the end of a text.
matched, err := regexp.MatchString(`^[0-9]*$`, "12three45")
fmt.Println(matched) // false
fmt.Println(err)     // nil (regexp is valid)

See this Regexp in-depth tutorial for a cheat sheet and plenty of code examples.

Share this page: