Golang正则模块使用

时间:2022-04-28
本文章向大家介绍Golang正则模块使用,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

最近在开发过程中会遇到一些字符串匹配相关的内容,正好去大概学习了下Golang中的regexp模块。因为目前正则模块对我来说更多的就是去匹配并处理字符串的,因此目前主要关注几个返回为string类型的方法。

regexp模块的结构体和方法定义

//正则结构体
type Regexp struct {
        // contains filtered or unexported fields
}

//初始化结构体对象的方法
func Compile(expr string) (*Regexp, error)
func CompilePOSIX(expr string) (*Regexp, error)
func MustCompile(str string) *Regexp
func MustCompilePOSIX(str string) *Regexp


//结构体方法.常用的几个
//在字符串s中查找完全匹配正则表达式re的字符串.如果匹配到就停止不进行全部匹配,如果匹配不到就输出空字符串
func (re *Regexp) FindString(s string) string

//在字符串s中匹配re表达式,n表示匹配的次数,-1表示匹配整个字符串。返回字符串切片
func (re *Regexp) FindAllString(s string, n int) []string

//在src中匹配re,并替换为repl,该种方式中repl中的$符号会展开实际的变量,通常用在回溯查找中
func (re *Regexp) ReplaceAllString(src, repl string) string

//在src中匹配re,并替换为repl,该方法会按照repl中的字面意思进行替换,不支持高级变量匹配,比如回溯等等
func (re *Regexp) ReplaceAllLiteralString(src, repl string) string


//在字符串中是否匹配到re定义的字符串,匹配返回true
func (re *Regexp) MatchString(s string) bool

简单示例

$ cat regexp-test.go
/**
 * @File Name: test.go
 * @Author:
 * @Email:
 * @Create Date: 2017-12-24 15:12:31
 * @Last Modified: 2017-12-24 16:12:12
 * @Description:
 */
package main

import (
	"fmt"
	"regexp"
)

func main() {
  testString := "k8s-test-pod-12343k811sadsxsakxz-test-k8s-container.k8s-@xxbandy.github.io"
	re := regexp.MustCompile("k8s?")
  fmt.Println("src string:",testString)
  fmt.Println("regular expression:",re)

  fmt.Println("FindAllString matching all:",re.FindAllString(testString,-1))
  fmt.Println("FindAllString matching twice:",re.FindAllString(testString,2))
  fmt.Println("FindString:",re.FindString(testString))
  fmt.Println("ReplaceAllString:",re.ReplaceAllString(testString,"biaoge"))
  fmt.Println("ReplaceAllLiteralString:",re.ReplaceAllLiteralString(testString,"BIAOGE"))
  fmt.Println("Match String:",re.MatchString(testString))
}

$ go run regexp-test.go
src string: k8s-test-pod-12343k811sadsxsakxz-test-k8s-container.k8s-@xxbandy.github.io
regular expression: k8s?
FindAllString matching all: [k8s k8 k8s k8s]
FindAllString matching twice: [k8s k8]
FindString: k8s
ReplaceAllString: biaoge-test-pod-12343biaoge11sadsxsakxz-test-biaoge-container.biaoge-@xxbandy.github.io
ReplaceAllLiteralString: BIAOGE-test-pod-12343BIAOGE11sadsxsakxz-test-BIAOGE-container.BIAOGE-@xxbandy.github.io
Match String: true

不论是哪种语言的正则模块,个人认为语法都不是最重要的,最重要我认为还是正则表达式本身,如果对正则表达式本身认识比较深的话,不论用哪种语言工具都可以很灵活的处理各种业务场景。这里附上一篇当年写的正则表达式相关的文章