Regex scenarios to Match Specific Version Number Format in Go - Handling Leading Zeros
I'm prototyping a solution and I'm following best practices but I'm struggling with crafting a regular expression in Go that can accurately match version numbers in the format `MAJOR.MINOR.PATCH` where each number can have leading zeros (e.g., `01.02.0003`). The scenario is that I want to ensure that the numbers can be up to three digits long but shouldn't allow any non-numeric characters. I've tried the following regex pattern: ```go pattern := `^\d{1,3}\.\d{1,3}\.\d{1,3}$` ``` However, this doesn't seem to validate leading zeros correctly. When I test it with a version string like `01.02.0003`, it fails to match, but `1.2.3` works fine. I also get unexpected behavior when trying to match `1.10.012`, where it should match but doesn't because my regex doesn't account for the leading zeros properly. I've looked into using capturing groups and different patterns but havenβt found a solution that handles both leading zeros and the three-digit limit effectively. Here's a snippet of the code Iβm using to test the regex: ```go package main import ( "fmt" "regexp" ) func main() { version := "01.02.0003" pattern := `^\d{1,3}\.\d{1,3}\.\d{1,3}$` matched, err := regexp.MatchString(pattern, version) if err != nil { fmt.Println("behavior: ", err) } fmt.Println("Matched: ", matched) // Should be true } ``` I need help refining the regex so that it correctly matches version numbers with leading zeros. Any suggestions on how to achieve this in a clean and efficient way? I'm using Go version 1.19. This is for a REST API running on CentOS. Thanks for your help in advance! This is for a service running on Debian. This is happening in both development and production on Ubuntu 20.04. I'm on Debian using the latest version of Go. Thanks, I really appreciate it!