Replacing parts of a string
You can use a regular expression to search through text, replacing parts that match a pattern with other strings.
How to do it...
Use the Replace
family of functions to replace the patterns in a string with something else:
package main import ( "fmt" "regexp" ) func main() { // Find numbers, capture the first digit re := regexp.MustCompile(`([0-9])[0-9]*`) fmt.Println(re.ReplaceAllString("This example replaces numbers with 'x': 1, 100, 500.", "x")) // This example replaces numbers with 'x': x, x, x. fmt.Println(re.ReplaceAllString("This example replaces all numbers with their first digits: 1, 100, 500...