Golang Regex:理解点'.'字符

1,131 阅读3分钟

概述

点'.'字符是正则表达式中最常使用的元字符之一。它被用来匹配任何字符。如果在正则表达式中加入一个特定的标志,它还可以匹配新的一行,我们将在后面讨论。默认情况下,它不匹配新行。

在研究正则表达式本身和圆点'.'字符的用法之前,让我们看看Go提供的一些基本函数或方法来进行正则匹配。

MatchCompile函数

golang.org/pkg/regexp/…。下面是这个函数的签名

func MustCompile(str string) *Regexp

我们首先使用MustCompile函数编译给定的regex字符串。如果给定的regex是无效的,这个函数就会慌乱。当它能够成功地编译给定的regex后,它返回regexp结构的实例。

sampleRegexp := regexp.MustCompile("some_regular_expression")

匹配方法

golang.org/pkg/regexp/…

下面是该方法的签名

func (re *Regexp) Match(b []byte) bool

我们可以在regexp结构实例上调用Match方法来匹配给定的模式和regex。如果regex与输入字符串匹配,则返回true,否则返回false。我们需要向该方法传入输入字符串的字节数。

match := sampleRegexp.Match([]byte("some_string"))

我们将在后面的例子中看到这两个函数的作用。

现在让我们看看一个关于点'.'字符的简单程序

package main

import (
	"fmt"
	"regexp"
)

func main() {
	sampleRegexp := regexp.MustCompile(".")

	match := sampleRegexp.Match([]byte("a"))
	fmt.Printf("For a: %t\n", match)

	match = sampleRegexp.Match([]byte("b"))
	fmt.Printf("For b: %t\n", match)

	match = sampleRegexp.Match([]byte("ab"))
	fmt.Printf("For ab: %t\n", match)

	match = sampleRegexp.Match([]byte(""))
	fmt.Printf("For empty string: %t\n", match)
}

输出

For a: true
For b: true
For ab: true
For empty string: false

在上面的程序中,我们有一个只包含一个点字符的简单的重合词。

sampleRegexp := regexp.MustCompile(".")

它匹配以下字符和字符串。

a
b
ab

匹配的是ab ,因为默认情况下,除非我们使用锚定字符(Caret和Dollar字符),否则该词不会匹配整个字符串。这就是为什么它匹配'ab'中的第一个字符'a'并报告匹配结果。

它不会匹配一个空字符串。

让我们看看另一个例子,在这个词中我们有两个点。

package main

import (
	"fmt"
	"regexp"
)

func main() {
	sampleRegexp := regexp.MustCompile("..")
	match := sampleRegexp.Match([]byte("ab"))
	fmt.Printf("For ab: %t\n", match)

	match = sampleRegexp.Match([]byte("ba"))
	fmt.Printf("For ba: %t\n", match)

	match = sampleRegexp.Match([]byte("abc"))
	fmt.Printf("For abc: %t\n", match)

	match = sampleRegexp.Match([]byte("a"))
	fmt.Printf("For a: %t\n", match)
}

输出

For ab: true
For ba: true
For abc: true
For a: false

在上面的程序中,我们有一个包含两个点的简单词组。

sampleRegexp := regexp.MustCompile("..")

它将匹配任何给定的、至少有两个字符作为子串的字符串。

这就是为什么它能匹配到

ab
ba
abc

而没有匹配到

a

正如我们之前提到的,点**'.'**也不匹配新行。但是默认行为可以通过在正则表达式的开头添加一组标志来改变。我们需要添加到regex开头的标志是。

(?s)

让我们看一个同样的程序

package main

import (
	"fmt"
	"regexp"
)

func main() {
	sampleRegexp := regexp.MustCompile(".")

	match := sampleRegexp.Match([]byte("\n"))
	fmt.Printf("For \\n: %t\n", match)

	sampleRegexp = regexp.MustCompile("(?s).")

	match = sampleRegexp.Match([]byte("\n"))
	fmt.Printf("For \\n: %t\n", match)
}

输出

For \n: false
For \n: true
sampleRegexp := regexp.MustCompile(".")

sampleRegexp = regexp.MustCompile("(?s).")

在第二个regex中,我们添加了额外的标志。这就是为什么它给出了一个新行的匹配,而第一个没有标志的regex却没有给出一个匹配。

将圆点作为一个字面字符使用

如果你想把点'.'作为一个字面字符使用,我们需要用反斜杠转义。一旦转义,它将匹配一个字面的点字符。 例如,如果我们想匹配下面的字面字符串或文本

a.b

那么相同的重码将是

a\.b

以下是相同的程序

package main

import (
	"fmt"
	"regexp"
)

func main() {
	sampleRegexp := regexp.MustCompile("a\\.b")

	match := sampleRegexp.Match([]byte("a.b"))

	fmt.Printf("For a.b string: %t\n", match)
}

输出

For a.b string: true

在一个字符类中的点字符

点或'.'被视为方括号或字符类内的字面字符。它在里面不需要被转义。让我们看看同样的工作程序

package main

import (
	"fmt"
	"regexp"
)

func main() {
	sampleRegexp := regexp.MustCompile("[.]")
	match := sampleRegexp.Match([]byte("."))

	fmt.Println(match)

}

输出

true
Also, check out our Golang advance tutorial Series – Golang Advance Tutorial

The postGolang Regex:了解点'.'字符出现在WelcomeTo Golang By Example