Find numbers in a string using the Golang regular expression

I want to find all the 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 regular expression, using a positive number as the second parameter for FindAllString, using only a string of numbers, such as "123" as the first parameter ...

But the way out is always []

I seem to have missed something about how regular expressions work in Go, but can't wrap their heads around it. Is the [0-9]+expression invalid?

+4
source share
1 answer

The problem is with your second integer argument. Quoting from the doc package regex:

, n; n >= 0, n /.

0, 0 ; : ( ).

-1, , .

:

re := regexp.MustCompile("[0-9]+")
fmt.Println(re.FindAllString("abc123def987asdf", -1))

:

[123 987]

Go Playground.

+16

Source: https://habr.com/ru/post/1610545/


All Articles