package main
import (
"fmt"
"regexp"
)
func test(){
s1 := `a\tb`
s2 := `a\\tb`
s3 := "a\tb"
s4 := "a\\tb"
fmt.Println(s1) //a\tb
fmt.Println(s2) //a\\tb
fmt.Println(s3) //a b
fmt.Println(s4) //a\tb
}
func main(){
str := "abc aac a.c a7c a c ac"
//反单引号表示 原义字符串
reg := regexp.MustCompile(`a.c`) //`a[0-9]c` `a\dc`
if(reg != nil){
s := reg.FindAllStringSubmatch(str,-1) //-1表示全部匹配
fmt.Println(s) //[[abc] [aac] [a.c] [a7c] [a c]]
}
test()
test2()
}
//提取出小数
func test2(){
buf := "3.14 345.12 3. aa.b 6 7.8"
reg := regexp.MustCompile(`\d+\.\d+`)
if(reg != nil){
s := reg.FindAllString(buf,-1)
fmt.Println(s) //[3.14 345.12 7.8]
ss := reg.FindAllStringSubmatch(buf,-1)
fmt.Println(ss) //[[3.14] [345.12] [7.8]]
}
}