内容简介:正则表达式,又称规则表达式,Regular Expression,在代码中常简写为 regex、regexp 或 RE。正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。 Perl 语言的正则表达式功能非常强大,很多语言设计正则式支持的时候都参考Perl的正则表达式。因此常用的表达式语法也是 Perl 兼容正则表达式。Go 语言中使用包通过编译正则表达式,可以得到正则操作对象,用于完成正则
1 概述
正则表达式,又称规则表达式,Regular Expression,在代码中常简写为 regex、regexp 或 RE。正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑。 Perl 语言的正则表达式功能非常强大,很多语言设计正则式支持的时候都参考 Perl 的正则表达式。因此常用的表达式语法也是 Perl 兼容正则表达式。
Go 语言中使用包 regexp
提供对正则表达式的支持。本文说明 regexp
中常用的正则处理方法。
2 获取正则对象
通过编译正则表达式,可以得到正则操作对象,用于完成正则的相关处理: 函数:
regexp.Compile(expr string) (*Regexp, error) regexp.MustCompile(str string) *Regexp
reg,err := regexp.Compile(`\d+`) 或 reg := regexp.MustCompile(`\d+`)
3 匹配检测
函数:
func (re *Regexp) MatchString(s string) bool func (re *Regexp) Match(b []byte) bool
演示字符串的匹配:
text := "Hello Gopher,Hello 韩忠康" reg := regexp.MustCompile(`\w+`) fmt.Println(reg.MatchString(text)) // true
4 查找
函数:
-
func (re *Regexp) FindString(s string) string
,查找匹配模式的字符串,返回左侧第一个匹配的结果。 -
func (re *Regexp) FindAllString(s string, n int) []string
,用来查找匹配模式的字符串,返回多个匹配的结果,n 用于限定查找数量,-1不限制。 -
func (re *Regexp) FindAll(b []byte, n int) [][]byte
,用于在[]byte
中查找,返回[][]byte
。
匹配全部结果演示为:
text := "Hello Gopher,Hello 韩忠康" reg := regexp.MustCompile(`\w+`) fmt.Println(reg.FindAllString(text)) // [Hello Gopher Hello]
5 查找匹配位置
以下函数用于获取匹配正则子字符串的位置:
func (re *Regexp) FindStringIndex(s string) (loc []int) func (re *Regexp) FindIndex(b []byte) (loc []int) func (re *Regexp) FindAllStringIndex(s string, n int) [][]int
演示查找字符串最左侧匹配位置:
text := "Hello Gopher,Hello 韩忠康" reg := regexp.MustCompile("llo") fmt.Println(reg.FindStringIndex(text)) // [2 5]
6 查找匹配子模式
以下函数可以查找子模式,或查找子模式的位置:
func (re *Regexp) FindStringSubmatch(s string) []string func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string func (re *Regexp) FindStringSubmatchIndex(s string) []int
演示匹配全部子字符串如下:
re := regexp.MustCompile("Go(\w+)") fmt.Println(re.FindAllStringSubmatch("Hello Gopher,Hello GoLang", -1)) // [["Gophoer" "phoer"], ["GoLang", "Lang"]]
7 替换
函数:
func (re *Regexp) ReplaceAllString(src, repl string) string func (re *Regexp) ReplaceAll(src, repl []byte) []byte
re := regexp.MustCompile("Go(\w+)") fmt.Println(re.ReplaceAllString("Hello Gopher,Hello GoLang", "Hank$1")) // Hello Hankpher,Hello HankLang
8 分割
函数:
-
func (re *Regexp) Split(s string, n int) []string
,使用正则分割字符串 s ,返回字符串切片。n 控制分割的片数,-1为不限制。
reg := regexp.MustCompile("[\s,]") fmt.Println(reg.Split("Hello Gopher,Hello GoLang", -1)) // [Hello Gopher Hello GoLang]
除了以上列举的较为常用的方法外,请参考 godoc -http=:8088
获取更全的信息。
完! 原文出自: 小韩说课 微信关注:小韩说课
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
High Performance Python
Micha Gorelick、Ian Ozsvald / O'Reilly Media / 2014-9-10 / USD 39.99
If you're an experienced Python programmer, High Performance Python will guide you through the various routes of code optimization. You'll learn how to use smarter algorithms and leverage peripheral t......一起来看看 《High Performance Python》 这本书的介绍吧!