1. 程式人生 > >Go語言之正則表達式

Go語言之正則表達式

GO 正則表達式

[TOC]

Go語言正則表達式

方式一:使用Compile

package main

import (
    "fmt"
    "regexp"
)

const text = "my email is [email protected]"

func main() {
    //re 是 正則表達式的匹配器
    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方法中的正則表達式,Go語言不知道是否正確,有可能用戶寫的正則表達式是錯誤的。

方式二:使用MustCompile方法

==好處就是,參數必須是正確的正則表達式==

例子1

package main

import (
    "fmt"
    "regexp"
)

const text_1 = "my email is [email protected]"

func main() {
    //目前的正則表達式,僅僅是匹配一個值,[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 main

import (
    "fmt"
    "regexp"
)

const text_1 = "my email is [email protected]"

func main() {
    //目前的正則表達式,僅僅是匹配一個值,[email protected]
    re := regexp.MustCompile("[email protected]")
    match := re.FindString(text_1)

    fmt.Println(match)
}

運行結果:

[email protected]

Process finished with exit code 0

==如何匹配正則表達式中一個點呢?==

如在點的前面,添加一個反斜杠\,  
但是,Go語言會將反斜杠當做是轉義字符,因此,需要添加兩個反斜杠 \\.  
同時,Go 語言,可以不使用"", 也可以使用反單引號,`` 來引用正則表達式,這樣的話,就不需要反斜杠了,

例子3

package main

import (
    "fmt"
    "regexp"
)

const text_3 = "my email is [email protected]"

func main() {
    //目前的正則表達式,僅僅是匹配一個值,[email protected]
    re := regexp.MustCompile(`.+@.+\..+`)
    match := re.FindString(text_3)

    fmt.Println(match)
}

運行結果:

my email is [email protected]

Process finished with exit code 0

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

例子4

package main

import (
    "fmt"
    "regexp"
)

const text_4 = "my email is [email protected]"

func main() {
    //只匹配小寫字母,大寫字母,數字,不允許有特殊符號
    re := regexp.MustCompile(`[a-zA-Z0-9]+@[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 main

import (
    "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]+@[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,如何提取出 名字,域名呢?

==正則表達式具有提取功能,只需要將要提取的字符,用小括號 括起來就可以了==

package main

import (
    "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語言之正則表達式