golang regexp记录

发布时间:2020-08-06 17:50:28 作者:暮色伊人
来源:网络 阅读:1438
  1. FindAllSubmatch与FindSubmatch区别
  // 匹配一个非元音字母,一个元音字母,一个非元音字母
    someRegex, _ := regexp.Compile(`[^aouiye]([aouiye])([^aouiye])?`)
    m1 := someRegex.FindAllStringSubmatch("somestri", -1)
    m2 := someRegex.FindStringSubmatch("somestri")

    fmt.Println(m1)
    fmt.Println(m2)

    //result:
    [[som o m] [ri i ]]
    [som o m]

    re2, _ := regexp.Compile("am(.*)lang(.*)")
    //查找Submatch,返回数组,第一个元素是匹配的全部元素,第二个元素是第一个()里面的,第三个是第二个()里面的
    //下面的输出第一个元素是"am learning Go language"
    //第二个元素是" learning Go ",注意包含空格的输出
    //第三个元素是"uage"
    submatch := re2.FindSubmatch([]byte(a))
    fmt.Println("FindSubmatch", submatch)
    for _, v := range submatch {
        fmt.Println(string(v))
    }

    //定义和上面的FindIndex一样
    submatchindex := re2.FindSubmatchIndex([]byte(a))
    fmt.Println("submatchindex:",submatchindex)

    //FindAllSubmatchIndex,查找所有字匹配的index
    submatchallindex := re2.FindAllSubmatchIndex([]byte(a), -1)
    fmt.Println("submatchallindex:",submatchallindex)

    //FindAllSubmatch,查找所有符合条件的子匹配
    submatchall := re2.FindAllSubmatch([]byte(a), -1)
    fmt.Println("submatchall:",submatchall)

    //result

  submatchindex: [2 25 4 17 21 25]
  submatchallindex: [[2 25 4 17 21 25]]
  submatchall: [[[97 109 32 108 101 97 114 110 105 110 103 32 71 111 32 108 97 110 103 117 97 103 101] [32 108 101 97 114 110 105 110 103 32 71 111 32] [117 97 103 101]]]
  1. 贪婪与非贪婪
  s := "图片(img=32,34)http://www.xiong.com/jpg(/img)图片(img=32,34)http://www.xiong.com/jpg(/img)"

    //非贪婪模式
    parse,_ := regexp.Compile("\\(.*?\\)")
    fmt.Println(parse.MatchString(s))
    fmt.Println(parse.FindString(s))
    fmt.Println(parse.ReplaceAllString(s,"+"))

    //result
    true
    (img=32,34) //最左最短匹配
    图片+http://www.xiong.com/jpg+图片+http://www.xiong.com/jpg+

    //贪婪模式
    parse,_ := regexp.Compile("\\(.*\\)")
    fmt.Println(parse.MatchString(s))
    fmt.Println(parse.FindString(s))
    fmt.Println(parse.ReplaceAllString(s,"+"))

    //result
    true
  (img=32,34)http://www.xiong.com/jpg(/img)图片(img=32,34)http://www.xiong.com/jpg(/img)
  图片+
  1. find vs findAll
  a := "I am learning Go language"
    re, _ := regexp.Compile("[a-z]{2,4}")
    //查找符合正则的第一个
    one := re.Find([]byte(a))
    fmt.Println("Find:", string(one))
    //查找符合正则的所有slice,n小于0表示返回全部符合的字符串,不然就是返回指定的长度
    all := re.FindAll([]byte(a), -1)
    fmt.Print("FindAll:")
    for i:= 0; i < len(all); i++{
        fmt.Print(string(all[i])+",")
    }

    //查找符合条件的index位置,开始位置和结束位置
    index := re.FindIndex([]byte(a))
    fmt.Println("FindIndex", index)

    //查找符合条件的所有的index位置,n同上
    allindex := re.FindAllIndex([]byte(a), -1)
    fmt.Println("FindAllIndex", allindex)

    //result
  Find: am
  FindAll:am,lear,ning,lang,uage

  FindIndex [2 4]
  FindAllIndex [[2 4] [5 9] [9 13] [17 21] [21 25]]
  1. capture group

    var myExp = regexp.MustCompile(`(?P<first>\d+)\.(\d+).(?P<second>\d+)`)
    fmt.Printf("%+v\n", myExp.FindAllStringSubmatch("1234.5678.9",-1))
    fmt.Printf("%+v", myExp.FindStringSubmatch("1234.5678.9"))
    
    //result
    
    [[1234.5678.9 1234 5678 9]]
    [1234.5678.9 1234 5678 9]
推荐阅读:
  1. golang emoji表情处理
  2. golang使用正则表达式使用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

golang regexp 正则

上一篇:【MySQL】SHOW ENGINE INNODB STATUS \G之Pages flushed up to的理解

下一篇:云上更安全-打造数字新基建的安全底座

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》