zoukankan      html  css  js  c++  java
  • 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\^$.*+?{}()[]|", "-"))
        // "----------------------"
    }
  • 相关阅读:
    bzoj2733 永无乡 平衡树按秩合并
    bzoj2752 高速公路 线段树
    bzoj1052 覆盖问题 二分答案 dfs
    bzoj1584 打扫卫生 dp
    bzoj1854 游戏 二分图
    bzoj3316 JC loves Mkk 二分答案 单调队列
    bzoj3643 Phi的反函数 数学 搜索
    有一种恐怖,叫大爆搜
    BZOJ3566 概率充电器 概率dp
    一些奇奇怪怪的过题思路
  • 原文地址:https://www.cnblogs.com/angelyan/p/11936166.html
Copyright © 2011-2022 走看看