Go语言的正则表式之regexp包

正则表达式都是大同小异,随便写几个案例:

// code_029_regexp project main.go
package main

import (
    "fmt"
    "regexp"
)

//Go中的正则表达式
func main() {
    //返回保管正则表达式所有不重叠的匹配结果的[]string切片。如果没有匹配到,会返回nil。
    //案例1
    context1 := "3.14 123123 .68 haha 1.0 abc 6.66 123."
    exp1 := regexp.MustCompile(`\d+\.\d+`)
    result1 := exp1.FindAllStringSubmatch(context1, -1)
    fmt.Printf("%v\n", result1)
    //案例2
    context2 := `
        标题
        
你过来啊
hello mike
你大爷
呵呵 ` exp2 := regexp.MustCompile(`
(.*?)
`) result2 := exp2.FindAllStringSubmatch(context2, -1) fmt.Printf("%v\n", result2) //案例3: context3 := ` 标题
你过来啊
hello mike go
你大爷
呵呵 ` exp3 := regexp.MustCompile(`
(?s:(.*?))
`) //这里包含空格和换行 result3 := exp3.FindAllStringSubmatch(context3, -1) fmt.Printf("%v\n", result3) //案例4: context4 := ` 标题
你过来啊
hello mike go
你大爷
呵呵 ` for _, text := range result4 { fmt.Println(text[0]) fmt.Println(text[1]) fmt.Println("===========\n") } }

当前名称:Go语言的正则表式之regexp包
文章网址:http://scjbc.cn/article/jieiho.html

其他资讯