mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 01:21:38 +08:00
update book code
This commit is contained in:
35
eBook/examples/chapter_9/pattern.go
Normal file
35
eBook/examples/chapter_9/pattern.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// pattern.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
searchIn := "John: 2578.34 William: 4567.23 Steve: 5632.18" // string to search
|
||||
pat := "[0-9]+.[0-9]+" // pattern search for in searchIn
|
||||
|
||||
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, "##.#") // replace pat with "##.#"
|
||||
fmt.Println(str)
|
||||
// using a function :
|
||||
str2 := re.ReplaceAllStringFunc(searchIn, f)
|
||||
fmt.Println(str2)
|
||||
|
||||
}
|
||||
/* Output:
|
||||
Match found!
|
||||
John: ##.# William: ##.# Steve: ##.#
|
||||
John: 5156.68 William: 9134.46 Steve: 11264.36
|
||||
*/
|
Reference in New Issue
Block a user