关于go:Go-快速入门指南-正则表达式

5次阅读

共计 1185 个字符,预计需要花费 3 分钟才能阅读完成。

概述

调用 regexp 包即可。

例子

是否匹配

package main

import (
    "fmt"
    "regexp"
)

func main() {match, err := regexp.MatchString("h[a-z]+.*d$", "hello world")
    if err != nil {panic(err)
    }
    fmt.Println(match)

    match, err = regexp.MatchString("h[a-z]+.*d$", "ello world")
    if err != nil {panic(err)
    }
    fmt.Println(match)
}

// $ go run main.go
// 输入如下
/**
  true
  false
*/

匹配所有子字符串

package main

import (
    "fmt"
    "regexp"
)

func main() {c, err := regexp.Compile("h[a-z]")
    if err != nil {panic(err)
    }

    res := c.FindAllString("hello world", -1)
    fmt.Printf("res = %v\n", res)

    res2 := c.FindAllString("hello world hi ha h1", -1)
    fmt.Printf("res2 = %v\n", res2)
}

// $ go run main.go
// 输入如下
/**
  res = [he]
  res2 = [he hi ha]
*/

替换所有子字符串

package main

import (
    "fmt"
    "regexp"
)

func main() {c, err := regexp.Compile("h[a-z]")
    if err != nil {panic(err)
    }

    res := c.ReplaceAll([]byte("hello world"), []byte("?"))
    fmt.Printf("res = %s\n", res)

    res2 := c.ReplaceAll([]byte("hello world hi ha h1"), []byte("?"))
    fmt.Printf("res2 = %s\n", res2)
}

// $ go run main.go
// 输入如下
/**
  res = ?llo world
  res2 = ?llo world ? ? h1
*/

匹配中文

package main

import (
    "fmt"
    "regexp"
)

func main() {match, err := regexp.MatchString("\\x{4e00}-\\x{9fa5}", "hello world")
    if err != nil {panic(err)
    }
    fmt.Println(match)

    match, err = regexp.MatchString("\\p{Han}+", "hello 世界")
    if err != nil {panic(err)
    }
    fmt.Println(match)
}

// $ go run main.go
// 输入如下
/**
  false
  true
*/

分割我

正文完
 0