<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                > 本章介紹正則表達式,使用這種模式可以實現對文本內容 校驗、解析、替換 [TOC] # 獲取解析對象 > 可通過Compile() 和 MushCompile()來獲取到解析對象,并進行下一步的正則匹配 ## Compile() > 返回 Regexp 對象以及是否錯誤信息 ~~~ text := `wang666` // 如果解析出錯,則會panic re, err := regexp.Compile(`[a-z]+`) if err != nil { return } match := re.FindString(text) fmt.Println(match) ~~~ ## MustCompile() > 返回 Regexp,解析表達式失敗則會panic ~~~ text := `wang666` // 如果解析出錯,則會panic re := regexp.MustCompile(`[a-z]+`) match := re.FindString(text) fmt.Println(match) ~~~ # 校驗是否匹配 ~~~ text := `111@qq.com` re := regexp.MustCompile(`^(\w+)@(\w+)\.(\w{3,5})$`) result := re.MatchString(text) if result == true{ fmt.Println("available") }else { fmt.Println("unavailable") } ~~~ # 查找匹配內容 ## 正則匹配單個值 ### FindString() 匹配單個值 > 如果沒有匹配的話,返回 空字符串 ~~~ text := `my email is 111@qq.com` re := regexp.MustCompile(`\w+@\w+\.\w{3,5}$`) match := re.FindString(text) fmt.Println(match) ~~~ > 運行結果 ~~~ 111@qq.com ~~~ ### FindStringSubmatch() 提取分組內容 > 就是提取正則表達式括號里的內容,如果沒有匹配的話,返回 nil ~~~ text := `my email is 111@qq.com` re := regexp.MustCompile(`(\w+)@(\w+)\.(\w{3,5})$`) match := re.FindStringSubmatch(text) fmt.Println(match) fmt.Println("--------------------") fmt.Printf("value %v , name=%v\n", match, match[1]) ~~~ > 運行結果 ~~~ value [111@qq.com 111 qq com] , name=111 ~~~ ## 正則匹配多個值 ### FindAllString() 匹配多值 > 如果沒有匹配的話,返回 nil ~~~ text := `my email is 111@qq.com my email2 is 222@qq.com` re := regexp.MustCompile(`\w+@\w+\.\w{3,5}`) matches := re.FindAllString(text, -1) fmt.Println(matches) fmt.Println("--------------------") for _, value := range matches { fmt.Println(value) } ~~~ > 運行結果 ~~~ [111@qq.com 222@qq.com] -------------------- 111@qq.com 222@qq.com ~~~ ### FindAllStringSubmatch() 提取分組內容 > 就是提取正則表達式括號里的內容,如果沒有匹配的話,返回 nil ~~~ text := `my email is 111@qq.com my email2 is 222@qq.com` re := regexp.MustCompile(`(\w+)@(\w+)\.(\w{3,5})`) matches := re.FindAllStringSubmatch(text, -1) fmt.Println(matches) fmt.Println("--------------------") for _, value := range matches { fmt.Printf("value %v , name=%v\n", value, value[1]) } ~~~ > 運行結果 ~~~ [[111@qq.com 111 qq com] [222@qq.com 222 qq com]] -------------------- value [111@qq.com 111 qq com] , name=111 value [222@qq.com 222 qq com] , name=222 ~~~ # 替換內容 > 使用ReplaceAllString() 進行全文本替換 ~~~ text := `my email is 111@qq.com my email2 is 222@qq.com` re := regexp.MustCompile(`\w+@\w+\.\w{3,5}`) newstr := re.ReplaceAllString(text, "***") // 還可以只替換分組部分,如下 //newstr := re.ReplaceAllString(text, `***@$2.$3`) fmt.Println(newstr) ~~~ > 運行結果 ~~~ my email is *** my email2 is *** ~~~ > 使用ReplaceAllStringFunc() 進行全文本替換 ~~~ text := `my email is 111@qq.com my email2 is 222@qq.com` re := regexp.MustCompile(`(\w+)@(\w+)\.(\w{3,5})`) newstr := re.ReplaceAllStringFunc(text, func(s string) string { str := s if s == "111@qq.com" { str = "[data is encrypted]" } return str }) fmt.Println(newstr) ~~~ > 運行結果 ~~~ my email is [data is encrypted] my email2 is 222@qq.com ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看