golang正規取得と置換

823 ワード

// test18 project main.go
package main

import (
	"fmt"
	"regexp"
	"strconv"
)

//           ,   IEEE754       。
// bigSize     32   64   ,         。
//        ,  err.Error = ErrSyntax
//         ,    ±Inf,err.Error = ErrRange
func main() {
	searchIn := "John: 2578.34 William: 4567.23 Steve: 5632.18"
	pat := "[0-9]+.[0-9]+" //  
	f := 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, "##.#")
	fmt.Println(str)
	//       
	str2 := re.ReplaceAllStringFunc(searchIn, f)
	fmt.Println(str2)
}