mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 06:36:43 +08:00
Update 09.2.md: 修复代码缩减问题 (#810)
Co-authored-by: qiujinfeng <qiujinfeng@qianxin.com>
This commit is contained in:
@@ -22,23 +22,25 @@ ok, _ := regexp.MatchString(pat, searchIn)
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//目标字符串
|
//目标字符串
|
||||||
searchIn := "John: 2578.34 William: 4567.23 Steve: 5632.18"
|
searchIn := "John: 2578.34 William: 4567.23 Steve: 5632.18"
|
||||||
pat := "[0-9]+.[0-9]+" //正则
|
pat := "[0-9]+.[0-9]+" //正则
|
||||||
|
|
||||||
f := func(s string) string{
|
f := func(s string) string {
|
||||||
v, _ := strconv.ParseFloat(s, 32)
|
v, _ := strconv.ParseFloat(s, 32)
|
||||||
return strconv.FormatFloat(v * 2, 'f', 2, 32)
|
return strconv.FormatFloat(v*2, 'f', 2, 32)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ok, _ := regexp.Match(pat, []byte(searchIn)); ok {
|
if ok, _ := regexp.Match(pat, []byte(searchIn)); ok {
|
||||||
fmt.Println("Match Found!")
|
fmt.Println("Match Found!")
|
||||||
}
|
}
|
||||||
|
|
||||||
re, _ := regexp.Compile(pat)
|
re, _ := regexp.Compile(pat)
|
||||||
|
Reference in New Issue
Block a user