golangregexp记录
                                            - 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]]]
- 贪婪与非贪婪
  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)
  图片+
- 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]]
- capture group  - 创新互联建站长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为宜阳企业提供专业的网站设计制作、成都网站制作,宜阳网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。 - var myExp = regexp.MustCompile(`(?P- \d+)\.(\d+).(?P - \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] 
新闻名称:golangregexp记录
本文来源:http://www.scyingshan.cn/article/iipiph.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 