The regexp Package
This lesson provides information about the regexp package and the functionalities it provides.
We'll cover the following...
Press + to interact
package mainimport ("fmt""regexp""strconv")func main() {searchIn := "John: 2578.34 William: 4567.23 Steve: 5632.18" // string to searchpat := "[0-9]+.[0-9]+" // pattern search in searchInf := func (s string) string {v, _ := strconv.ParseFloat(s, 32)return strconv.FormatFloat(v * 2, 'f', 2, 32)}if ok, _ := regexp.Match(pat, []byte(searchIn)); ok {fmt.Println("Match found!")}re, _ := regexp.Compile(pat)str := re.ReplaceAllString(searchIn, "##.#") // replace pat with "##.#"fmt.Println(str)// using a functionstr2 := re.ReplaceAllStringFunc(searchIn, f)fmt.Println(str2)}
In the code above, outside main
...