I want to find all numbers in a string with the following code:
re:=regexp.MustCompile("[0-9]+")
fmt.Println(re.FindAllString("abc123def", 0))
I also tried adding delimiters to the regex, using a positive number as second parameter for FindAllString
, using a numbers only string like "123" as first parameter...
But the output is always []
I seem to miss something about how regular expressions work in Go, but cannot wrap my head around it. Is [0-9]+
not a valid expression?
The problem is with your second integer argument. Quoting from the package doc of
regex
:You pass
0
so at most 0 matches will be returned; that is: none (not really useful).Try passing
-1
to indicate you want all.Example:
Output:
Try it on the Go Playground.