golang 正则 regexp包使用

Python014

golang 正则 regexp包使用,第1张

先介绍几种常用的方法:

1、使用MatchString函数或Match函数

regexp.MatchString(pattern string, s string) pattern为正则表达式,s为需要校验的字符串

regexp.Match(pattern string, b []byte) pattern为正则表达式,s为需要校验的字符串

它们的作用都是匹配,区别在于参数为字符串和切片

实例如下:

2、使用 Compile函数或MustCompile函数

它们的区别是Compile返回两个参数 Regexp,error类型,而MustCompile只返回 Regexp类型

它们的作用是将正则表达式进行编译,返回优化的 Regexp 结构体,该结构体有需多方法。

实例如下:

3、查找正则匹配字串( 注:函数名包含string的所传参数为string 其他的均为[]byte 带All是所有)

查找正则匹配的字符串位置( 注:函数名包含string的所传参数为string 其他的均为[]byte 带All是所有)

4、替换

正则替换

按原文替换

函数处理替换源字串

5、Regexp结构体中一些常用的方法

正则中有分组这个功能,在golang中也可以使用命名分组。

一次匹配的情况

场景还原如下:

有一行文本,格式为:姓名 年龄 邮箱地址

请将其转换为一个map

代码实现如下:

str := `Alice 20 [email protected]`

// 使用命名分组,显得更清晰

re := regexp.MustCompile(`(?P<name>[a-zA-Z]+)\s+(?P<age>\d+)\s+(?P<email>\w+@\w+(?:\.\w+)+)`)

match := re.FindStringSubmatch(str)

groupNames := re.SubexpNames()

fmt.Printf("%v, %v, %d, %d\n", match, groupNames, len(match), len(groupNames))

result := make(map[string]string)

// 转换为map

for i, name := range groupNames {

if i != 0 &&name != "" { // 第一个分组为空(也就是整个匹配)

result[name] = match[i]

}

}

prettyResult, _ := json.MarshalIndent(result, "", " ")

fmt.Printf("%s\n", prettyResult)

输出为:

[Alice 20 [email protected] Alice 20 [email protected]], [ name age email], 4, 4

{

"age": "20",

"email": "[email protected]",

"name": "Alice"

}

注意 [ name age email]有4个元素, 第一个为""。

多次匹配的情况

接上面的例子,实现一个更贴近现实的需求:

有一个文件, 内容大致如下:

Alice 20 [email protected]

Bob 25 [email protected]

gerrylon 26 [email protected]

...

更多内容

和上面一样, 不过这次转出来是一个slice of map, 也就是多个map。

代码如下:

// 文件内容直接用字符串表示

usersStr := `

Alice 20 [email protected]

Bob 25 [email protected]

gerrylon 26 [email protected]

`

userRe := regexp.MustCompile(`(?P<name>[a-zA-Z]+)\s+(?P<age>\d+)\s+(?P<email>\w+@\w+(?:\.\w+)+)`)

// 这里要用FindAllStringSubmatch,找到所有的匹配

users := userRe.FindAllStringSubmatch(usersStr, -1)

groupNames := userRe.SubexpNames()

var result []map[string]string // slice of map

// 循环所有行

for _, user := range users {

m := make(map[string]string)

// 对每一行生成一个map

for j, name := range groupNames {

if j != 0 &&name != "" {

m[name] = strings.TrimSpace(user[j])

}

}

result = append(result, m)

}

prettyResult, _ := json.MarshalIndent(result, "", " ")

fmt.Println(string(prettyResult))

输出为:

[

{

"age": "20",

"email": "[email protected]",

"name": "Alice"

},

{

"age": "25",

"email": "[email protected]",

"name": "Bob"

},

{

"age": "26",

"email": "[email protected]",

"name": "gerrylon"

}

]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

总结

使用命名分组可以使正则表示的意义更清晰。

转换为map更加符合人类的阅读习惯,不过比一般的根据索引取分组值麻烦一些。

————————————————

版权声明:本文为CSDN博主「butterfly5211314」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/butterfly5211314/article/details/82532970

go语言有支持正则表达式后向引用的方法,方法如下

package main

import (

"fmt"

"os"

"path/filepath"

"regexp"

)

func main() {

// 命令行参数

args := os.Args

// 检查参数

if len(args) == 1 {

fmt.Println("ff is a file find tool. use like bottom")

fmt.Println("ff [dir] [regexp]")

return

}

if len(args) <3 {

fmt.Println("args <3")

return

}

fileName := args[1]

pattern := args[2]

file, err := os.Open(fileName)

if err != nil {

fmt.Println(err)

return

}

fi, err := file.Stat()

if err != nil {

fmt.Println(err)

return

}

if !fi.IsDir() {

fmt.Println(fileName, " is not a dir")

}

reg, err := regexp.Compile(pattern)

if err != nil {

fmt.Println(err)

return

}

// 遍历目录

filepath.Walk(fileName,

func(path string, f os.FileInfo, err error) error {

if err != nil {

fmt.Println(err)

return err

}

if f.IsDir() {

return nil

}

// 匹配目录

matched := reg.MatchString(f.Name())

if matched {

fmt.Println(path)

}

return nil

})

}