常见正则表达式

2021-08-08  本文已影响0人  彳亍口巴

元字符

参考链接:正则表达式
正则表达式的测试:JavaScript正则表达式在线测试工具
表1.常用的元字符:

代码 说明
. 匹配除换行符以外的任意字符
\w 匹配字母或数字或下划线或汉字
\s 匹配任意的空白符
\d 匹配数字
\b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束

表2 常用的限定符

代码/语法 说明
* 重复零次或更多次
+ 重复一次或更多次
? 重复零次或一次
{n} 重复n次
{n,} 重复n次或更多次
{n,m} 重复n到m次

分支条件:从左到右进行匹配

代码/语法 说明
\W 匹配任意不是字母,数字,下划线,汉字的字符
\S 匹配任意不是空白符的字符
\D 匹配任意非数字的字符
\B 匹配不是单词开头或结束的位置
[^x] 匹配除了x以外的任意字符
[^aeiou] 匹配除了aeiou这几个字母以外的任意字符

go代码

package main

import (
    "fmt"
    "regexp"
)

func main() {
test02()
}

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

// 字符串匹配
func test01() {
    //text:=`Hello 世界!123 Go.`
    flag, err := regexp.MatchString(`\b\d\b`, "1")
    if err != nil {
        fmt.Println("err=", err)
        return
    }
    fmt.Println("flag==", flag)
}

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

    // 查找连续的英文字母
    reg := regexp.MustCompile(`[a-zA-Z]+`)
    fmt.Printf("%q\n", reg.FindAllString(text, -1))
    // ["Hello" "Go"]
}
上一篇 下一篇

猜你喜欢

热点阅读