likes
comments
collection
share

Go 语言入门很简单:Go 正则表达式

作者站长头像
站长
· 阅读数 8

前言

在计算中,我们经常需要将特定模式的字符或字符子集匹配为另一个字符串中的字符串。此技术用于使用特别的语法来搜索给定字符串中的特定字符集。比如邮件、手机号、身份证号等等。

如果搜索到的模式匹配,或者在目标字符串中找到给定的子集,则搜索被称为成功;否则被认为是不成功的。

那么此时该用到正则表达式了。

什么是正则表达式

正则表达式(或 RegEx)是一个特殊的字符序列,它定义了用于匹配特定文本的搜索模式。在 Golang 中,有一个内置的正则表达式包: regexp 包,其中包含所有操作列表,如过滤、修改、替换、验证或提取。

正则表达式可以用于文本搜索和更高级的文本操作。正则表达式内置于 grep 和 sed 等工具,vi 和 emacs 等文本编辑器,Go、Java 和 Python 等编程语言中。表达式的语法主要遵循这些流行语言中使用的已建立的 RE2 语法。 RE2 语法是 PCRE 的一个子集,有各种注意事项。

以下是正则表达式模式表格:

RegexMeaning
.匹配任何单一字符
?匹配前面的元素一次要么根本不匹配。
+匹配前面的元素一次或多次。
*匹配前一个元素的零次或多次。
^匹配字符串中的起始位置。
$匹配字符串中的结束位置。
|交替运算符。
[abc]匹配 a 或 b,或 c。
[a-c]a-c 的范围; 匹配 a 或 b,或 c。
[^abc]否定, 匹配除 a,或 b,或 c之外的一切。
\s匹配空格字符。
\w匹配一个单词字符;等同于 [a-zA-Z_0-9]

Go 语言的 regexp 包中有几个典型的函数:

  • MatchString()
  • Compile()
  • FindString()
  • FindAllString()
  • FindStringIndex()
  • FindAllStringIndex()
  • FindStringSubmatch()
  • Split()
  • ReplaceAllString
  • ReplaceAllStringFunc()

MatchString 函数

MatchString() 函数用来匹配子字符串,下面的程序用来匹配是否是 一个字母后跟着 even 字符,正则模式为: .even

package main

import (
	"fmt"
	"log"
	"regexp"
)

func main() {

	words := [...]string{"Seven", "even", "Maven", "Amen", "eleven"}

	for _, word := range words {

		found, err := regexp.MatchString(".even", word)

		if err != nil {
			log.Fatal(err)
		}

		if found {

			fmt.Printf("%s matches\n", word)
		} else {

			fmt.Printf("%s does not match\n", word)
		}
	}
}

运行该代码:

Seven matches
even does not match
Maven does not match
Amen does not match
eleven matches

但同时我们能看到编辑器有提示,说明直接用 MatchString 匹配字符串会影响性能。:

Go 语言入门很简单:Go 正则表达式

所以我们考虑用 Compile() 或者 MustCompile()创建一个编译好的正则表达式对象,然后再来进行模式匹配。

Compile 函数

Compile 函数解析正则表达式,如果成功,则返回可用于匹配文本的 Regexp 对象。编译的正则表达式产生更快的代码。假如正则表达式非法,那么 Compile() 方法回返回 error ,而 MustCompile() 编译非法正则表达式时不会返回 error ,而是返回 panic 。

先来看 Compile 函数:

package main

import (
	"fmt"
	"log"
	"regexp"
)

func main() {

	words := [...]string{"Seven", "even", "Maven", "Amen", "eleven"}

	re, err := regexp.Compile(".even")

	if err != nil {
		log.Fatal(err)
	}

	for _, word := range words {

		found := re.MatchString(word)

		if found {

			fmt.Printf("%s matches\n", word)
		} else {

			fmt.Printf("%s does not match\n", word)
		}
	}
}

在代码示例中,我们使用了编译的正则表达式。

re, err := regexp.Compile(".even")

即使用 Compile 编译正则表达式。然后在返回的正则表达式对象上调用 MatchString 函数:

found := re.MatchString(word)

运行程序,能看到同样的代码:

Seven matches
even does not match
Maven does not match
Amen does not match
eleven matches

MustCompile 函数

再看使用 MustCompile 函数的例子,它编译正则表达式并在无法解析表达式时发生 panic。

package main

import (
	"fmt"
	"regexp"
)

func main() {

	words := [...]string{"Seven", "even", "Maven", "Amen", "eleven"}

	re := regexp.MustCompile(".even")

	for _, word := range words {

		found := re.MatchString(word)

		if found {

			fmt.Printf("%s matches\n", word)
		} else {

			fmt.Printf("%s does not match\n", word)
		}
	}
}

运行该代码:

$ go run main.go 
Seven matches
even does not match 
Maven does not match
Amen does not match 
eleven matches 

FindString 查找字符串

FindString() 用来返回第一个匹配的结果。如果没有匹配的字符串,那么它回返回一个空的字符串,当然如果你的正则表达式就是要匹配空字符串的话,它也会返回空字符串。使用 FindStringIndex 或者 FindStringSubmatch可以区分这两种情况。下面是FindString()的例子:

package main

import (
	"fmt"
	"regexp"
)

func main() {

	str := "Today is Tuesday!"

	regexp, _ := regexp.Compile("^T([a-z]+)y")

	fmt.Println(regexp.FindString(str))
}

运行该代码,可以看到如下结果:

$ go run main.go 
Today

FindAllString 函数

FindAllString 函数返回正则表达式的所有连续匹配的切片。

package main

import (
    "fmt"
    "os"
    "regexp"
)

func main() {

    var content = `Foxes are omnivorous mammals belonging to several genera 
of the family Canidae. Foxes have a flattened skull, upright triangular ears, 
a pointed, slightly upturned snout, and a long bushy tail. Foxes live on every 
continent except Antarctica. By far the most common and widespread species of 
fox is the red fox.`

    re := regexp.MustCompile("(?i)fox(es)?")

    found := re.FindAllString(content, -1)

    fmt.Printf("%q\n", found)

    if found == nil {
        fmt.Printf("no match found\n")
        os.Exit(1)
    }

    for _, word := range found {
        fmt.Printf("%s\n", word)
    }

}

在代码示例中,我们找到了单词 fox 的所有出现,包括它的复数形式。

re := regexp.MustCompile("(?i)fox(es)?")

使用 (?i) 语法,正则表达式不区分大小写。 (es)?表示“es”字符可能包含零次或一次。

found := re.FindAllString(content, -1)

我们使用 FindAllString 查找所有出现的已定义正则表达式。第二个参数是要查找的最大匹配项; -1 表示搜索所有可能的匹配项。

运行结果:

["Foxes" "Foxes" "Foxes" "fox" "fox"]
Foxes
Foxes
Foxes
fox
fox

FindAllStringIndex 函数

package main

import (
    "fmt"
    "regexp"
)

func main() {

    var content = `Foxes are omnivorous mammals belonging to several genera 
of the family Canidae. Foxes have a flattened skull, upright triangular ears, 
a pointed, slightly upturned snout, and a long bushy tail. Foxes live on every 
continent except Antarctica. By far the most common and widespread species of 
fox is the red fox.`

    re := regexp.MustCompile("(?i)fox(es)?")

    idx := re.FindAllStringIndex(content, -1)

    for _, j := range idx {
        match := content[j[0]:j[1]]
        fmt.Printf("%s at %d:%d\n", match, j[0], j[1])
    }
}

在代码示例中,我们在文本中找到所有出现的 fox 单词及其索引。

Foxes at 0:5
Foxes at 81:86
Foxes at 196:201
fox at 296:299
fox at 311:314

Split 函数

Split 函数将字符串切割成由定义的正则表达式分隔的子字符串。它返回这些表达式匹配之间的子字符串切片。

package main

import (
    "fmt"
    "log"
    "regexp"
    "strconv"
)

func main() {

    var data = `22, 1, 3, 4, 5, 17, 4, 3, 21, 4, 5, 1, 48, 9, 42`

    sum := 0

    re := regexp.MustCompile(",\s*")

    vals := re.Split(data, -1)

    for _, val := range vals {

        n, err := strconv.Atoi(val)

        sum += n

        if err != nil {
            log.Fatal(err)
        }
    }

    fmt.Println(sum)
}

在代码示例中,我们有一个逗号分隔的值列表。我们从字符串中截取值并计算它们的总和。

re := regexp.MustCompile(",\s*")

正则表达式包括一个逗号字符和任意数量的相邻空格。

vals := re.Split(data, -1)

我们得到了值的一部分。

for _, val := range vals {

    n, err := strconv.Atoi(val)

    sum += n

    if err != nil {
        log.Fatal(err)
    }
}

我们遍历切片并计算总和。切片包含字符串;因此,我们使用 strconv.Atoi 函数将每个字符串转换为整数。

运行代码:

189

Go 正则表达式捕获组

圆括号 () 用于创建捕获组。这允许我们将量词应用于整个组或将交替限制为正则表达式的一部分。 为了找到捕获组(Go 使用术语子表达式),我们使用 FindStringSubmatch 函数。

package main

import (
    "fmt"
    "regexp"
)

func main() {

    websites := [...]string{"webcode.me", "zetcode.com", "freebsd.org", "netbsd.org"}

    re := regexp.MustCompile("(\w+)\.(\w+)")

    for _, website := range websites {

        parts := re.FindStringSubmatch(website)

        for i, _ := range parts {
            fmt.Println(parts[i])
        }

        fmt.Println("---------------------")
    }
}

在代码示例中,我们使用组将域名分为两部分。

re := regexp.MustCompile("(\w+)\.(\w+)")

我们用括号定义了两个组。

parts := re.FindStringSubmatch(website)

FindStringSubmatch 返回包含匹配项的字符串切片,包括来自捕获组的字符串。

运行代码:

$ go run capturegroups.go 
webcode.me
webcode
me
---------------------
zetcode.com
zetcode
com
---------------------
freebsd.org
freebsd
org
---------------------
netbsd.org
netbsd
org
---------------------

正则表达式替换字符串

可以用 ReplaceAllString 替换字符串。该方法返回修改后的字符串。

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "regexp"
    "strings"
)

func main() {

    resp, err := http.Get("http://webcode.me")

    if err != nil {
        log.Fatal(err)
    }

    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)

    if err != nil {

        log.Fatal(err)
    }

    content := string(body)

    re := regexp.MustCompile("<[^>]*>")
    replaced := re.ReplaceAllString(content, "")

    fmt.Println(strings.TrimSpace(replaced))
}

该示例读取网页的 HTML 数据并使用正则表达式去除其 HTML 标记。

resp, err := http.Get("http://webcode.me")

我们使用 http 包中的 Get 函数创建一个 GET 请求。

body, err := ioutil.ReadAll(resp.Body)

我们读取响应对象的主体。

re := regexp.MustCompile("<[^>]*>")

这个模式定义了一个匹配 HTML 标签的正则表达式。

replaced := re.ReplaceAllString(content, "")

我们使用 ReplaceAllString 方法删除所有标签。

ReplaceAllStringFunc 函数

ReplaceAllStringFunc 返回一个字符串的副本,其中正则表达式的所有匹配项都已替换为指定函数的返回值。

package main

import (
    "fmt"
    "regexp"
    "strings"
)

func main() {

    content := "an old eagle"

    re := regexp.MustCompile(`[^aeiou]`)

    fmt.Println(re.ReplaceAllStringFunc(content, strings.ToUpper))
}

在代码示例中,我们将 strings.ToUpper 函数应用于字符串的所有字符。

$ go run replaceallfunc.go 
aN oLD eaGLe

总结

模式匹配在根据基于正则表达式和语法的特定搜索模式在字符串中搜索某些字符集时起着重要作用。匹配的模式允许我们从字符串中提取所需的数据并以我们喜欢的方式对其进行操作。理解和使用正则表达式是处理文本的关键。

在实际过程中,程序员会保留一组常用的正则表达式来匹配电子邮件、电话号码等,并在需要时使用和重用它。

本文只是简单挑选了一些常见的函数进行介绍,其实 regexp 包还有更多的函数,留给读者自己去探索和研究。

参考链接: