Regular expression to extract numbers from a string in Golang
Example
package main
import (
"fmt"
"regexp"
)
func main() {
str1 := "Hello X42 I'm a Y-32.35 string Z30"
re := regexp.MustCompile(`[-]?\d[\d,]*[\.]?[\d{2}]*`)
fmt.Printf("Pattern: %v\n", re.String()) // Print Pattern
fmt.Printf("String contains any match: %v\n", re.MatchString(str1)) // True
submatchall := re.FindAllString(str1, -1)
for _, element := range submatchall {
fmt.Println(element)
}
}
Output
Pattern: [-]?\d[\d,]*[\.]?[\d{2}]*
String contains any match: true
42
-32.35
30
Most Helpful This Week
How to play and pause execution of goroutine?
How to find length of Map in Go?
Convert specific UTC date time to PST, HST, MST and SGT
Golang Read Write Create and Delete text file
Example: Arrays of Arrays, Arrays of Slices, Slices of Arrays and Slices of Slices
Regular expression for matching HH:MM time format in Golang
Example of Fscan, Fscanf, and Fscanln from FMT Package
How to get struct variable information using reflect package?
How to read input from console line?
How to iterate over an Array using for loop?