1. 程式人生 > >Go語言中正則表達式的使用

Go語言中正則表達式的使用

Go語言正則表達式 Go語言正則表達式的使用 Go語言 正則表達式

許多程序語言都支持使用正則表達式對字符串進行操作,Go語言也不例外,正則表達式的語法網上很多教程,本文主要介紹在Go語言中如何使用正則表達式,通過以下實例進行說明,並添加了詳細的註釋,不會的小夥伴一看就明白了。

func main() {
    // 要操作的字符串
    str := "abcde 1234 [email protected] [email protected] [email protected]"

    // 正則表達式字符串----用於匹配郵箱地址,來源於網絡
    regexStr := "([A-Za-z0-9\u4e00-\u9fa5]+)@([a-zA-Z0-9_-]+)(\\.[a-zA-Z0-9_-]+)+"

    // 定義正則表達式
    match := regexp.MustCompile(regexStr)
    // 或者
    //match,err := regexp.Compile(regexStr)
    // 或者
    //match := regexp.MustCompile(regexStr)

    // 判斷是否存在正則表達式匹配的字符串========方法1
    // 此處用於判斷str中是否存在郵箱地址
    isExist := match.MatchString(str)
    fmt.Println(isExist)
    // 執行結果:true

    // 判斷是否存在正則表達式匹配的字符串========方法2
    // 此處用於判斷"8888888888"中是否存在郵箱地址
    isExist1, _ := regexp.MatchString(regexStr, "8888888888")
    fmt.Println(isExist1)
    // 執行結果:false

    // 查詢左邊第一次匹配的字符串
    // 此處用於查詢str中左邊第一個郵箱地址
    findS1 := match.FindString(str)
    fmt.Println(findS1)
    // 執行結果:[email protected]

    // 查詢從左邊開始匹配的n個字符串,為-1時,則全部匹配,返回是一個string切片
    // 此處用於查詢str中左邊開始2個郵箱地址
    findS2 := match.FindAllString(str, 2)
    fmt.Printf("%T====%v\n", findS2, findS2)
    // 執行結果:[]string====[[email protected] [email protected]]

    // 查詢從左邊開始匹配的第一個字符串,返回的是下標,是一個int切片
    // 此處用於查詢str中左邊第一個郵箱地址在str中的下標
    findS3 := match.FindStringIndex(str)
    fmt.Printf("%T====%v\n", findS3, findS3)
    // 執行結果:[]int====[11 21]
    fmt.Println(str[findS3[0] : findS3[1]])
    // 執行結果:[email protected]

    // 子匹配,只匹配從左邊開始的第一個字符串,在正則表達式中每一個小括號裏的內容為一個子串,返回一個string切片
    // 此處用於匹配str中左邊第一個郵箱地址,並匹配第一個郵箱地址中的子串
    findS4 := match.FindStringSubmatch(str)
    fmt.Printf("%T====%v\n", findS4, findS4)
    // 執行結果:[]string====[[email protected] my 163 .com]
    // 其中my和163及.com都是子串

    // 子匹配,只匹配從左邊開始的第一個字符串,返回一個記錄下標的int切片
    // 此處用於匹配str中左邊第一個郵箱及其子串的下標
    findS5 := match.FindStringSubmatchIndex(str)
    fmt.Printf("%T====%v\n", findS5, findS5)
    // 執行結果:[]int====[11 21 11 13 14 17 17 21]
    // 其中11 21是[email protected]的下標,11 13是my的下標,14 17是163的下標,17 21是.com的下標

    // 子匹配,匹配從左邊開始的n個字符串,為-1時則匹配所有,返回一個二維string切片
    // 此處用於匹配str中所有郵箱地址,及這些郵箱地址中的子串
    findS6 := match.FindAllStringSubmatch(str, -1)
    fmt.Printf("%T====%v\n", findS6, findS6)
    // 執行結果:[][]string====[[[email protected] my 163 .com] [[email protected] 386832092 qq .com] [[email protected] admin hotmail .com]]

    // 子匹配,匹配從左邊開始的n個字符串,為-1時則匹配所有,返回一個記錄下標的二維int切片
    // 此處用於匹配str中所有郵箱地址及這些郵箱地址中的子串的下標
    findS7 := match.FindAllStringSubmatchIndex(str, -1)
    fmt.Printf("%T====%v\n", findS7, findS7)
    // 執行結果:[][]int====[[11 21 11 13 14 17 17 21] [22 38 22 31 32 34 34 38] [39 56 39 44 45 52 52 56]]

    // 使用byte切片作為參數和返回值
    // 此處用於匹配str中第一個郵箱地址,以byte切片作為參數和返回值
    findS8 := match.Find([]byte(str))
    fmt.Printf("%T====%v====%s\n", findS8, findS8, findS8)
    執行結果:[]uint8====[109 121 64 49 54 51 46 99 111 109][email protected]

  // 使用byte切片作為參數,判斷是否有匹配的字符串
    // 此處用於判斷str中是否存在郵箱地址,以byte切片為參數
    findS9 := match.Match([]byte(str))
    fmt.Printf("%T====%v\n", findS9, findS9)
    // 執行結果:bool====true

    // 字符串替換,返回替換後的字符串
    // 此處用於將str中所有郵箱地址替換為"<email>"
    findS10 := match.ReplaceAllString(str, "<email>")
    fmt.Printf("%T====%v\n", findS10, findS10)
    // 執行結果:string====abcde 1234 <email> <email> <email>

    // 字符串替換,使用函數作為替換參數
    // 此處用於將str中的所有郵箱地址替換為下面replace的返回值
    findS11 := match.ReplaceAllStringFunc(str, replace)
    fmt.Printf("%T====%v\n", findS11, findS11)
    // 執行結果:string====abcde 1234 my的郵箱 你好中國 你好中國

    // 字符串替換,用byte切片,使用函數作替換參數
    // 此處用於將str中的郵箱地址全部替換為大寫字母,以byte切片為參數和返回值
    findS12 := match.ReplaceAllFunc([]byte(str), bytes.ToUpper)
    fmt.Printf("%T====%v====%s\n", findS12, findS12, findS12)
    // 執行結果:[]uint8====[97 98 99 100 101 32 49 50 51 52 32 77 89 64 49 54 51 46 67 79 77 32 51 56 54 56 51 50 48 57 50 64 81 81 46 67 79 77 32 65 68 77 73 78 64 72 79 84 77 65 73 76 46 67 79 77]====abcde 1234 [email protected] [email protected] [email protected]

    // \p{Unicode腳本類名}  Unicode類 (腳本類)    Han----中文
    // 此處用於將上面"abcde 1234 my的郵箱 你好中國 你好中國"中的所有漢字查詢出來
    match1 := regexp.MustCompile("\\p{Han}")
    findS13 := match1.FindAllString(findS11, -1)
    fmt.Printf("%T====%v\n", findS13, findS13)
    // 執行結果:[]string====[的 郵 箱 你 好 中 國 你 好 中 國]
}

func replace(str string) string {
    if strings.EqualFold(str, "[email protected]") {
        return "my的郵箱"
    }
    return "你好中國"
}

Go語言中正則表達式的使用