golang regexp記錄

  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]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章