• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

go语言中使用正则表达式

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一、代码

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\\^$.*+?{}()[]|", "-"))
    // "----------------------"
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
GO入门——2. 变量发布时间:2022-07-10
下一篇:
编译protobuf文件生成go代码时添加自定义的fieldtag发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap