Go语言学习#4-正则表达式


=Start=

缘由:

最近在跟着郝林老师的《Go语言核心36讲》这门课程重拾Go语言学习(之前在15年的时候我自学过一段时间,不过后来没怎么用Go来写代码,所以有些内容都忘了,这次希望能坚持下去),所以准备把一些常见功能用Go语言试着实践一遍,加强学习效果和对这门语言的感觉。

正文:

参考解答:
  • func Compile(expr string) (*Regexp, error) //Compile在解析表达式成功的情况下会返回一个Regexp对象
  • func MustCompile(str string) *Regexp //MustCompile和Compile很像,但是会在表达式无法解析的情况下抛出panic
  • func MatchString(pattern string, s string) (matched bool, err error) //MatchString返回true/false表示是否匹配
  • func (re *Regexp) FindString(s string) string //FindString返回一个字符串,该字符串保存通过正则表达式最左匹配到的文本如果没有匹配,则返回值是空字符串,但是如果正则表达式成功匹配空字符串,则返回值也是空的。如果需要区分这些情况,请使用FindStringIndex或FindStringSubmatch方法
  • func (re *Regexp) FindAllString(s string, n int) []string //FindAllString返回一个包含所有匹配项的字符串切片
  • func (re *Regexp) Split(s string, n int) []string //Split用传入的正则表达式表示分隔符对字符串进行切分,返回一个字符串切片作为结果

 

s := regexp.MustCompile("a*").Split("abaabaccadaaae", 5)
// s: ["", "b", "b", "c", "cadaaae"]

re := regexp.MustCompile("fo.?")
fmt.Printf("%q\n", re.FindString("seafood")) // "foo"
fmt.Printf("%q\n", re.FindString("meat")) // ""

&

package main

import (
    "fmt"
    "regexp"
)

func main() {
    // someRegex, _ := regexp.Compile(`[^aouiye]([aouiye])([^aouiye])?`) // 注意「反引号」
    // matches := someRegex.FindAllStringSubmatch("somestri", -1)
    // fmt.Printf("%q\n", matches)

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

 

参考链接:

=END=

, ,

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注