• go语言中使用正则表达式


    一、代码

    package main
    
    import (
        "fmt"
        "regexp"
    )
    
    
    func main() {
        text := `Hello 世界!123 Go.`
    
        // 查找连续的小写字母
        reg := regexp.MustCompile(`[a-z]+`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["ello" "o"]
    
        // 查找连续的非小写字母
        reg = regexp.MustCompile(`[^a-z]+`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["H" " 世界!123 G" "."]
    
        // 查找连续的单词字母
        reg = regexp.MustCompile(`[w]+`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["Hello" "123" "Go"]
    
        // 查找连续的非单词字母、非空白字符
        reg = regexp.MustCompile(`[^ws]+`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["世界!" "."]
    
        // 查找连续的大写字母
        reg = regexp.MustCompile(`[[:upper:]]+`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["H" "G"]
    
        // 查找连续的非 ASCII 字符
        reg = regexp.MustCompile(`[[:^ascii:]]+`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["世界!"]
    
        // 查找连续的标点符号
        reg = regexp.MustCompile(`[pP]+`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["" "."]
    
        // 查找连续的非标点符号字符
        reg = regexp.MustCompile(`[PP]+`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["Hello 世界" "123 Go"]
    
        // 查找连续的汉字
        reg = regexp.MustCompile(`[p{Han}]+`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["世界"]
    
        // 查找连续的非汉字字符
        reg = regexp.MustCompile(`[P{Han}]+`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["Hello " "!123 Go."]
    
        // 查找 Hello 或 Go
        reg = regexp.MustCompile(`Hello|Go`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["Hello" "Go"]
    
        // 查找行首以 H 开头,以空格结尾的字符串
        reg = regexp.MustCompile(`^H.*s`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["Hello 世界!123 "]
    
        // 查找行首以 H 开头,以空白结尾的字符串(非贪婪模式)
        reg = regexp.MustCompile(`(?U)^H.*s`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["Hello "]
    
        // 查找以 hello 开头(忽略大小写),以 Go 结尾的字符串
        reg = regexp.MustCompile(`(?i:^hello).*Go`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["Hello 世界!123 Go"]
    
        // 查找 Go.
        reg = regexp.MustCompile(`QGo.E`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["Go."]
    
        // 查找从行首开始,以空格结尾的字符串(非贪婪模式)
        reg = regexp.MustCompile(`(?U)^.* `)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["Hello "]
    
        // 查找以空格开头,到行尾结束,中间不包含空格字符串
        reg = regexp.MustCompile(` [^ ]*$`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // [" Go."]
    
        // 查找“单词边界”之间的字符串
        reg = regexp.MustCompile(`(?U).+`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["Hello" " 世界!" "123" " " "Go"]
    
        // 查找连续 1 次到 4 次的非空格字符,并以 o 结尾的字符串
        reg = regexp.MustCompile(`[^ ]{1,4}o`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["Hello" "Go"]
    
        // 查找 Hello 或 Go
        reg = regexp.MustCompile(`(?:Hell|G)o`)
        fmt.Printf("%q
    ", reg.FindAllString(text, -1))
        // ["Hello" "Go"]
    
        // 查找 Hello 或 Go,替换为 Hellooo、Gooo
        reg = regexp.MustCompile(`(?:o)`)
        fmt.Printf("%q
    ", reg.ReplaceAllString(text, "${n}ooo"))
        // "Hellooo 世界!123 Gooo."
    
        // 交换 Hello 和 Go
        reg = regexp.MustCompile(`(Hello)(.*)(Go)`)
        fmt.Printf("%q
    ", reg.ReplaceAllString(text, "$3$2$1"))
        // "Go 世界!123 Hello."
    
        // 特殊字符的查找
        reg = regexp.MustCompile(`[f	
    
    v123x7Fx{10FFFF}\^$.*+?{}()[]|]`)
        fmt.Printf("%q
    ", reg.ReplaceAllString("f	
    
    v123x7FU0010FFFF\^$.*+?{}()[]|", "-"))
        // "----------------------"
    }
  • 相关阅读:
    cmd常用命令
    快捷键
    leetcode:Sort List(一个链表的归并排序)
    leetcode:Merge Two Sorted Lists(有序链表的归并)
    leetcode:Search for a Range(数组,二分查找)
    leetcode:Search a 2D Matrix(数组,二分查找)
    实时操作系统与分时操作系统
    第四章函数作业题,函数模板
    第四章函数作业题,函数的重载
    第四章函数作业题,内置函数
  • 原文地址:https://www.cnblogs.com/angelyan/p/11936166.html
Copyright © 2020-2023  润新知