Go語言之Regex

來源:互聯網
上載者:User

標籤:GO   Regex   

[TOC]

Go語言Regex方式一:使用Compile
package mainimport (    "fmt"    "regexp")const text = "my email is [email protected]"func main() {    //re 是 Regex的匹配器    re, err := regexp.Compile("[email protected]")    if err != nil {        panic(err)    }    result := re.FindString(text)    fmt.Println("result:\t", result)}

運行結果:

result:  [email protected]Process finished with exit code 0  

==此方法式,存在的問題?==
    Compile方法中的Regex,Go語言不知道是否正確,有可能使用者寫的Regex是錯誤的。

方式二:使用MustCompile方法

==好處就是,參數必須是正確的Regex==

例子1
package mainimport (    "fmt"    "regexp")const text_1 = "my email is [email protected]"func main() {    //目前的Regex,僅僅是匹配一個值,[email protected]    re := regexp.MustCompile("[email protected]")    match := re.FindString(text_1)    fmt.Println(match)}

運行結果:

[email protected]Process finished with exit code 0  

==問題 .+ 與 .*的區別==

. 表示可以匹配任何字元  .+ 表示可以匹配1以上的字元,也就是說,只少有一個字元  .* 表示可以匹配0個以上的字元,也就是說,0個以上字元  其實,+,* 都是匹配的數量
例子2
package mainimport (    "fmt"    "regexp")const text_1 = "my email is [email protected]"func main() {    //目前的Regex,僅僅是匹配一個值,[email protected]    re := regexp.MustCompile("[email protected]")    match := re.FindString(text_1)    fmt.Println(match)}

運行結果:

[email protected]Process finished with exit code 0

==如何匹配Regex中一個點呢?==

如在點的前面,添加一個反斜線\,  但是,Go語言會將反斜線當做是逸出字元,因此,需要添加兩個反斜線 \\.  同時,Go 語言,可以不使用"", 也可以使用反單引號,`` 來引用Regex,這樣的話,就不需要反斜線了,
例子3
package mainimport (    "fmt"    "regexp")const text_3 = "my email is [email protected]"func main() {    //目前的Regex,僅僅是匹配一個值,[email protected]    re := regexp.MustCompile(`[email protected]+\..+`)    match := re.FindString(text_3)    fmt.Println(match)}

運行結果:

my email is [email protected]Process finished with exit code 0

==存在問題?==
將這條語句全部列印出來,而不是僅僅符合要求的哪些欄位

例子4
package mainimport (    "fmt"    "regexp")const text_4 = "my email is [email protected]"func main() {    //只匹配小寫字母,大寫字母,數字,不允許有特殊符號    re := regexp.MustCompile(`[a-zA-Z0-9][email protected][a-zA-Z0-9]+\.[a-zA-Z0-9]+`)    match := re.FindString(text_4)    fmt.Println(match)}

運行結果:

[email protected]Process finished with exit code 0  
例子5 匹配多個時,如何處理?
package mainimport (    "fmt"    "regexp")const text_5 = `    my email is [email protected]    my email is [email protected]    my email is [email protected]    my email is [email protected]    my email is [email protected]`func main() {    //在[]裡,  . 不需要 添加 逸出字元    re := regexp.MustCompile(`[a-zA-Z0-9][email protected][a-zA-Z0-9]+\.[a-zA-Z0-9]+`)    //-1 表示,要匹配所有滿足條件的詞    match := re.FindAllString(text_5, -1)    fmt.Println(match)}

運行結果:

[[email protected] [email protected] [email protected] [email protected] [email protected]]Process finished with exit code 0
例子6,如何提取出 名字,網域名稱呢?

==Regex具有提取功能,只需要將要提取的字元,用小括弧 括起來就可以了==

package mainimport (    "fmt"    "regexp")const text_6 = `    my email is [email protected]    my email is [email protected]    my email is [email protected]    my email is [email protected]    my email is [email protected]`func main() {    //在[]裡,  . 不需要 添加 逸出字元    re := regexp.MustCompile(`([a-zA-Z0-9]+)@([a-zA-Z0-9]+)(\.[a-zA-Z0-9.]+)`)    //-1 表示,要匹配所有滿足條件的詞    match := re.FindAllStringSubmatch(text_6, -1)    for _, value := range match {        fmt.Println(value)    }}

運行結果:

[[email protected] k8sAndDocker google .com][[email protected] spark qq .com][[email protected] hadoop 126 .com][[email protected] kafka 163 .com][[email protected] docker 163docker .com.cn]Process finished with exit code 0   












Go語言之Regex

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.