go語言中使用正則表達式


一、代碼

package main

import (
    "fmt"
    "regexp"
)


func main() {
    text := `Hello 世界!123 Go.`

    // 查找連續的小寫字母
    reg := regexp.MustCompile(`[a-z]+`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["ello" "o"]

    // 查找連續的非小寫字母
    reg = regexp.MustCompile(`[^a-z]+`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["H" " 世界!123 G" "."]

    // 查找連續的單詞字母
    reg = regexp.MustCompile(`[\w]+`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["Hello" "123" "Go"]

    // 查找連續的非單詞字母、非空白字符
    reg = regexp.MustCompile(`[^\w\s]+`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["世界!" "."]

    // 查找連續的大寫字母
    reg = regexp.MustCompile(`[[:upper:]]+`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["H" "G"]

    // 查找連續的非 ASCII 字符
    reg = regexp.MustCompile(`[[:^ascii:]]+`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["世界!"]

    // 查找連續的標點符號
    reg = regexp.MustCompile(`[\pP]+`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["" "."]

    // 查找連續的非標點符號字符
    reg = regexp.MustCompile(`[\PP]+`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["Hello 世界" "123 Go"]

    // 查找連續的漢字
    reg = regexp.MustCompile(`[\p{Han}]+`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["世界"]

    // 查找連續的非漢字字符
    reg = regexp.MustCompile(`[\P{Han}]+`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["Hello " "!123 Go."]

    // 查找 Hello 或 Go
    reg = regexp.MustCompile(`Hello|Go`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["Hello" "Go"]

    // 查找行首以 H 開頭,以空格結尾的字符串
    reg = regexp.MustCompile(`^H.*\s`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["Hello 世界!123 "]

    // 查找行首以 H 開頭,以空白結尾的字符串(非貪婪模式)
    reg = regexp.MustCompile(`(?U)^H.*\s`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["Hello "]

    // 查找以 hello 開頭(忽略大小寫),以 Go 結尾的字符串
    reg = regexp.MustCompile(`(?i:^hello).*Go`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["Hello 世界!123 Go"]

    // 查找 Go.
    reg = regexp.MustCompile(`\QGo.\E`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["Go."]

    // 查找從行首開始,以空格結尾的字符串(非貪婪模式)
    reg = regexp.MustCompile(`(?U)^.* `)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["Hello "]

    // 查找以空格開頭,到行尾結束,中間不包含空格字符串
    reg = regexp.MustCompile(` [^ ]*$`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // [" Go."]

    // 查找“單詞邊界”之間的字符串
    reg = regexp.MustCompile(`(?U)\b.+\b`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["Hello" " 世界!" "123" " " "Go"]

    // 查找連續 1 次到 4 次的非空格字符,並以 o 結尾的字符串
    reg = regexp.MustCompile(`[^ ]{1,4}o`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["Hello" "Go"]

    // 查找 Hello 或 Go
    reg = regexp.MustCompile(`(?:Hell|G)o`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["Hello" "Go"]

    // 查找 Hello 或 Go,替換為 Hellooo、Gooo
    reg = regexp.MustCompile(`(?:o)`)
    fmt.Printf("%q\n", reg.ReplaceAllString(text, "${n}ooo"))
    // "Hellooo 世界!123 Gooo."

    // 交換 Hello 和 Go
    reg = regexp.MustCompile(`(Hello)(.*)(Go)`)
    fmt.Printf("%q\n", reg.ReplaceAllString(text, "$3$2$1"))
    // "Go 世界!123 Hello."

    // 特殊字符的查找
    reg = regexp.MustCompile(`[\f\t\n\r\v\123\x7F\x{10FFFF}\\\^\$\.\*\+\?\{\}\(\)\[\]\|]`)
    fmt.Printf("%q\n", reg.ReplaceAllString("\f\t\n\r\v\123\x7F\U0010FFFF\\^$.*+?{}()[]|", "-"))
    // "----------------------"
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM