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
Example to use Weekday and YearDay function
Golang Get current Date and Time in EST, UTC and MST?
Regular expression to validate email address
How to convert Go struct to JSON?
Regular Expression to get a string between parentheses in Golang
Example of Pointers with Struct
How to delete or remove element from a Map?
Strip all white spaces, tabs, newlines from a string
Runtime package variables
Different ways to convert Byte Array into String