package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?m)(?<!\d,)(?<!\d)\d{1,3}(?:,\d{3})*(?!,?\d)`)
var str = `I need help finding a regex rule that should search in a large string/text and match numbers that have the format: 12,345,678 or 1,234,567 or 12,345 or 1,234.
for example: for 12,345,678 it should match 12,345,678 and not 345,678 or 45,678 or anything similar
I looked in: Regex help needed to match numbers but the answers either match 1 in 1,23,456 (should not at all because 1,23,456 is not a number) or match 23,456 in 12,23,456 (should not match at all)
In creating a regex rule to match the correct format number, I tried first creating the rule of what it should not match(i.e., not 1,23,456), then I tried creating the rule of what it should match. The last rule I created matches in most cases, but not in all.
MATCH:
1 12 123 1,234 12,345 123,456 1,234,567 12,
NO MATCH:
1,24,567 1,234,5 1,2 1,2,567 12567 `
for i, match := range re.FindAllString(str, -1) {
fmt.Println(match, "found at index", i)
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Golang, please visit: https://golang.org/pkg/regexp/