golang 正则表达式用法与示例 – wiki基地


Go 语言中的正则表达式:强大、高效的文本匹配利器

在处理文本数据时,模式匹配、查找、替换和分割是常见的操作。正则表达式(Regular Expression,简称 Regex 或 Regexp)正是解决这类问题的强大工具。它使用一种紧凑的、形式化的语法来描述字符串的模式,可以用来检查一个字符串是否符合某种模式,提取字符串中符合模式的部分,或者替换匹配的模式。

Go 语言标准库提供了 regexp 包,为开发者提供了高效且功能丰富的正则表达式处理能力。与许多其他语言(如 Perl、Python、Java)中常用的基于回溯(Backtracking)的正则表达式引擎不同,Go 的 regexp 包使用的是基于有限状态自动机(Finite State Automaton,简称 FSA)的实现,具体来说是 Google 的 RE2 库的 Go 实现。RE2 的一个主要优势是它保证了在线性时间复杂度内完成匹配(相对于输入字符串的长度),这避免了某些复杂模式可能导致的“灾难性回溯”问题,从而提高了安全性和可预测性,尤其是在处理不受信任的输入时。虽然 RE2 的功能集比一些现代的、基于回溯的引擎(如 PCRE)稍有限制(例如,不支持某些高级特性如后向引用捕获组),但它覆盖了绝大多数常用的正则表达式功能,并且在性能和安全性方面表现出色。

本文将详细介绍 Go 语言 regexp 包的用法,包括基本的匹配、查找、替换、分割,以及正则表达式的语法、编译、性能考量和一些高级用法。

1. 导入 regexp

使用 Go 的正则表达式功能,首先需要导入 regexp 包:

go
import "regexp"

2. 基本用法:匹配(Matching)

最简单的用法是检查一个字符串是否包含某个模式。regexp 包提供了 MatchString 函数用于此目的。

go
func MatchString(pattern string, s string) (matched bool, err error)

这个函数接受一个正则表达式模式字符串和一个待匹配的字符串 s 作为输入,返回一个布尔值指示是否匹配成功,以及一个可能的错误(如果正则表达式模式本身无效)。

示例 1:检查字符串是否包含数字

“`go
package main

import (
“fmt”
“regexp”
)

func main() {
pattern := \d+ // 匹配一个或多个数字

s1 := "hello 123 world"
matched1, err1 := regexp.MatchString(pattern, s1)
if err1 != nil {
    fmt.Println("Error:", err1)
    return
}
fmt.Printf("String '%s' contains number: %v\n", s1, matched1) // Output: String 'hello 123 world' contains number: true

s2 := "hello world"
matched2, err2 := regexp.MatchString(pattern, s2)
if err2 != nil {
    fmt.Println("Error:", err2)
    return
}
fmt.Printf("String '%s' contains number: %v\n", s2, matched2) // Output: String 'hello world' contains number: false

// 模式错误示例
invalidPattern := `[` // 无效的模式:未闭合的字符集
s3 := "test"
_, err3 := regexp.MatchString(invalidPattern, s3)
if err3 != nil {
    fmt.Println("Error with invalid pattern:", err3) // Output: Error with invalid pattern: error parsing regexp: missing closing ]: `[`
}

}
“`

MatchString 内部实际上会先编译正则表达式,然后再进行匹配。如果需要在同一个模式上进行多次匹配,重复调用 MatchString 会导致模式被重复编译,效率较低。这时应该使用编译后的正则表达式对象(*regexp.Regexp)。

3. 编译正则表达式

为了提高性能,尤其是在循环中或需要多次使用同一个模式时,应该先将正则表达式模式编译成一个 *regexp.Regexp 对象。

regexp 包提供了两个函数用于编译:

  • regexp.Compile(pattern string) (*regexp.Regexp, error): 编译模式,返回一个 *regexp.Regexp 对象和可能的错误。这是推荐的方式,因为它允许你处理模式编译失败的情况。
  • regexp.MustCompile(pattern string) *regexp.Regexp: 编译模式。如果模式无效,它会 panic。通常用于包初始化阶段或确定模式是硬编码且绝对有效的情况下。

示例 2:编译和使用 *regexp.Regexp 对象

“`go
package main

import (
“fmt”
“regexp”
)

func main() {
pattern := go+ // 匹配一个 ‘g’ 后跟一个或多个 ‘o’

// 编译正则表达式
re, err := regexp.Compile(pattern)
if err != nil {
    fmt.Println("Error compiling regex:", err)
    return
}

// 使用编译后的对象进行匹配
s1 := "goooooogle"
matched1 := re.MatchString(s1)
fmt.Printf("'%s' matches '%s': %v\n", s1, pattern, matched1) // Output: 'goooooogle' matches 'go+': true

s2 := "goland"
matched2 := re.MatchString(s2)
fmt.Printf("'%s' matches '%s': %v\n", s2, pattern, matched2) // Output: 'goland' matches 'go+': true

s3 := "golang" // 匹配 'go' 后跟 'lang',不符合 'go+'
matched3 := re.MatchString(s3)
fmt.Printf("'%s' matches '%s': %v\n", s3, pattern, matched3) // Output: 'golang' matches 'go+': true (Wait, why true? Ah, `MatchString` checks if *any* substring matches. 'go' matches 'go+'. Let's adjust the pattern or example to clarify.)

// Let's adjust the example to make it clearer that the pattern must *appear* in the string.
// The previous example is actually correct for `MatchString`. Let's use a different pattern.
pattern2 := `^\d{3}$` // 精确匹配由3个数字组成的字符串

re2 := regexp.MustCompile(pattern2) // 使用 MustCompile,假设模式是硬编码且正确的

s4 := "123"
matched4 := re2.MatchString(s4)
fmt.Printf("'%s' matches '%s': %v\n", s4, pattern2, matched4) // Output: '123' matches '^\d{3}$': true

s5 := "12345"
matched5 := re2.MatchString(s5)
fmt.Printf("'%s' matches '%s': %v\n", s5, pattern2, matched5) // Output: '12345' matches '^\d{3}$': false

s6 := "abc"
matched6 := re2.MatchString(s6)
fmt.Printf("'%s' matches '%s': %v\n", s6, pattern2, matched6) // Output: 'abc' matches '^\d{3}$': false

}
“`

从现在开始,我们将主要使用编译后的 *regexp.Regexp 对象来演示各种操作。

4. 查找(Finding)

除了简单的匹配,正则表达式更常用于从文本中提取符合模式的部分。*regexp.Regexp 对象提供了一系列 Find 方法。

  • FindString(s string) string: 查找输入字符串 s 中第一个匹配模式的子字符串,并返回该子字符串。如果没有找到匹配,返回空字符串 ""
  • FindAllString(s string, n int) []string: 查找输入字符串 s 中所有匹配模式的子字符串,并返回一个字符串切片。参数 n 控制返回的匹配次数:n > 0 返回最多 n 个匹配,n <= 0 返回所有匹配。
  • 还有对应的 FindIndex, FindAllIndex 方法,它们返回匹配子字符串的起始和结束位置索引([]int 类型)。
  • 以及针对字节切片([]byte)的 Find, FindAll 方法。

示例 3:查找匹配项

“`go
package main

import (
“fmt”
“regexp”
)

func main() {
text := “The quick brown fox jumps over the lazy dog. The fox is agile.”
pattern := fox // 匹配单词 “fox”

re := regexp.MustCompile(pattern)

// 查找第一个匹配
firstMatch := re.FindString(text)
fmt.Printf("First match for '%s': '%s'\n", pattern, firstMatch) // Output: First match for 'fox': 'fox'

// 查找所有匹配 (n <= 0)
allMatches := re.FindAllString(text, -1)
fmt.Printf("All matches for '%s': %v\n", pattern, allMatches) // Output: All matches for 'fox': [fox fox]

// 查找最多 n 个匹配 (n = 1)
limitedMatches := re.FindAllString(text, 1)
fmt.Printf("Up to 1 match for '%s': %v\n", pattern, limitedMatches) // Output: Up to 1 match for 'fox': [fox]

// 查找不存在的模式
pattern2 := `cat`
re2 := regexp.MustCompile(pattern2)
noMatch := re2.FindString(text)
fmt.Printf("First match for '%s': '%s' (Empty string indicates no match)\n", pattern2, noMatch) // Output: First match for 'cat': '' (Empty string indicates no match)

}
“`

5. 捕获组(Capturing Groups)与提取信息

正则表达式中的圆括号 () 用于创建捕获组。捕获组不仅匹配文本,还会“记住”匹配到的内容,方便后续提取。

*regexp.Regexp 对象提供了 FindStringSubmatchFindAllStringSubmatch 方法来查找匹配,并同时捕获组的内容。

  • FindStringSubmatch(s string) []string: 查找第一个匹配,返回一个字符串切片。切片的第一个元素是整个匹配到的子字符串,后续元素是各个捕获组匹配到的内容。如果没有匹配,返回 nil
  • FindAllStringSubmatch(s string, n int) [][]string: 查找所有匹配,返回一个字符串切片的切片。每个内部切片结构同 FindStringSubmatch。参数 n 的含义与 FindAllString 相同。

示例 4:使用捕获组提取日期信息

假设我们要从格式为 YYYY-MM-DD 的字符串中提取年、月、日。

“`go
package main

import (
“fmt”
“regexp”
)

func main() {
dateString := “今天是 2023-10-27,明天是 2023-10-28。”
// 模式解释:
// (\d{4}) – 第一个捕获组,匹配4个数字 (年份)
// – – 匹配连字符
// (\d{2}) – 第二个捕获组,匹配2个数字 (月份)
// – – 匹配连字符
// (\d{2}) – 第三个捕获组,匹配2个数字 (日期)
pattern := (\d{4})-(\d{2})-(\d{2})

re := regexp.MustCompile(pattern)

// 查找第一个日期并提取各部分
firstMatchAndGroups := re.FindStringSubmatch(dateString)
fmt.Printf("First match and groups: %v\n", firstMatchAndGroups)
// Output: First match and groups: [2023-10-27 2023 10 27]
// 索引 0: 整个匹配到的字符串 "2023-10-27"
// 索引 1: 第一个捕获组 (\d{4}) 匹配到的 "2023"
// 索引 2: 第二个捕获组 (\d{2}) 匹配到的 "10"
// 索引 3: 第三个捕获组 (\d{2}) 匹配到的 "27"

if len(firstMatchAndGroups) > 0 {
    fmt.Printf("  Full match: %s\n", firstMatchAndGroups[0])
    fmt.Printf("  Year: %s\n", firstMatchAndGroups[1])
    fmt.Printf("  Month: %s\n", firstMatchAndGroups[2])
    fmt.Printf("  Day: %s\n", firstMatchAndGroups[3])
}

// 查找所有日期并提取各部分
allMatchesAndGroups := re.FindAllStringSubmatch(dateString, -1)
fmt.Printf("\nAll matches and groups:\n")
for _, match := range allMatchesAndGroups {
    fmt.Printf("  Match: %v\n", match)
    if len(match) > 0 {
        fmt.Printf("    Full match: %s, Year: %s, Month: %s, Day: %s\n",
            match[0], match[1], match[2], match[3])
    }
}
/* Output:
All matches and groups:
  Match: [2023-10-27 2023 10 27]
    Full match: 2023-10-27, Year: 2023, Month: 10, Day: 27
  Match: [2023-10-28 2023 10 28]
    Full match: 2023-10-28, Year: 2023, Month: 10, Day: 28
*/

// 如果没有匹配,FindStringSubmatch 返回 nil
noMatch := re.FindStringSubmatch("这是一个没有日期的字符串")
fmt.Printf("\nMatch for no date: %v (nil)\n", noMatch) // Output: Match for no date: [] (Actually nil, prints as [])
// Check with nil explicitly:
if noMatch == nil {
    fmt.Println("  FindStringSubmatch returned nil when no match.")
}

}
“`

非捕获组 (?:...)

有时候我们只需要将一部分模式组合在一起进行量词限定或分支选择,但不需要捕获它的内容。这时可以使用非捕获组 (?:...)。这可以稍微提高性能,并使捕获组的索引更清晰。

示例 5:非捕获组

“`go
package main

import (
“fmt”
“regexp”
)

func main() {
// 模式匹配 “cat” 或 “dog”,后面跟着感叹号,但只捕获 animal 部分
// (cat|dog) 是一个捕获组
// ! 是字面量
patternCapture := (cat|dog)!
reCapture := regexp.MustCompile(patternCapture)
matchCapture := reCapture.FindStringSubmatch(“I have a dog!”)
fmt.Printf(“With capture group: %v (Matches: %s, Captured: %s)\n”,
matchCapture, matchCapture[0], matchCapture[1]) // Output: With capture group: [dog! dog] (Matches: dog!, Captured: dog)

// 模式匹配 "cat" 或 "dog",后面跟着感叹号,只匹配整个模式
// (?:cat|dog) 是一个非捕获组
patternNonCapture := `(?:cat|dog)!`
reNonCapture := regexp.MustCompile(patternNonCapture)
matchNonCapture := reNonCapture.FindStringSubmatch("I have a dog!")
fmt.Printf("With non-capture group: %v (Matches: %s, No captures)\n",
    matchNonCapture, matchNonCapture[0]) // Output: With non-capture group: [dog!] (Matches: dog!, No captures)
// 注意 matchNonCapture 只有索引 0 (整个匹配),没有索引 1

}
“`

6. 替换(Replacing)

regexp 包提供了替换匹配到的子字符串的功能。

  • ReplaceAllString(src, repl string) string: 将输入字符串 src 中所有匹配模式的子字符串替换为 repl。在 repl 中,可以使用 $n${n} 引用第 n 个捕获组匹配的内容。$0${0} 代表整个匹配到的子字符串。
  • ReplaceAllStringFunc(src string, repl func(string) string) string: 使用一个函数 repl 来决定每个匹配项的替换内容。函数的输入是匹配到的子字符串,输出是替换后的字符串。

示例 6:使用 $n 替换

“`go
package main

import (
“fmt”
“regexp”
)

func main() {
text := “Email me at [email protected] or [email protected]
// 模式匹配简单的电子邮件地址 (忽略复杂性)
// (\S+) 捕获用户名 (非空白字符)
// @ 匹配 @
// (\S+) 捕获域名 (非空白字符)
pattern := (\S+)@(\S+)

re := regexp.MustCompile(pattern)

// 将所有邮箱地址替换为 [email protected]
replacedText1 := re.ReplaceAllString(text, "[email protected]")
fmt.Println("Replace with static string:", replacedText1) // Output: Replace with static string: Email me at [email protected] or [email protected]

// 将所有邮箱地址格式改为 domain:user
// $2 引用第二个捕获组 (域名)
// $1 引用第一个捕获组 (用户名)
replacedText2 := re.ReplaceAllString(text, "$2:$1")
fmt.Println("Replace with captured groups:", replacedText2) // Output: Replace with captured groups: Email me at example.com:john.doe or sample.net:jane.doe

// $0 代表整个匹配
replacedText3 := re.ReplaceAllString(text, "[$0]")
fmt.Println("Replace with full match:", replacedText3) // Output: Replace with full match: Email me at [[email protected]] or [[email protected]]

}
“`

示例 7:使用函数替换

“`go
package main

import (
“fmt”
“regexp”
“strings”
)

func main() {
text := “Price is $100 and tax is $15. Total: $115”
pattern := \$\d+ // 匹配以 $ 开头后跟一个或多个数字的金额

re := regexp.MustCompile(pattern)

// 定义一个函数,将匹配到的金额(如 $100)转换为 (100 USD)
replaceFunc := func(match string) string {
    // match 会是 "$100", "$15", "$115"
    // 去掉开头的 $
    amount := strings.TrimPrefix(match, "$")
    return fmt.Sprintf("(%s USD)", amount)
}

replacedText := re.ReplaceAllStringFunc(text, replaceFunc)
fmt.Println("Replace using a function:", replacedText) // Output: Replace using a function: Price is (100 USD) and tax is (15 USD). Total: (115 USD)

// ReplaceAllStringFunc 接收的函数参数是完整的匹配字符串。
// 如果你需要访问捕获组的内容,你需要自己解析match字符串,
// 或者先使用FindStringSubmatch获取分组信息,再手动构建替换逻辑。
// 对于更复杂的替换,考虑先FindAllStringSubmatch,然后手动构建新字符串。

}
“`

7. 分割(Splitting)

regexp 包的 Split 方法允许你使用正则表达式作为分隔符来分割字符串。

  • Split(s string, n int) []string: 使用模式匹配到的子字符串作为分隔符,将输入字符串 s 分割成一个字符串切片。参数 n 控制返回的子字符串数量:n > 0 返回最多 n 个子字符串(最后一个子字符串包含剩余未分割的部分),n == 0 返回 nil(表示不分割),n < 0 返回所有可能的子字符串。

示例 8:分割字符串

“`go
package main

import (
“fmt”
“regexp”
)

func main() {
csvData := “Apple,Banana,Cherry,,Date”
// 模式匹配逗号
patternComma := ,
reComma := regexp.MustCompile(patternComma)

// 按逗号分割
parts1 := reComma.Split(csvData, -1)
fmt.Printf("Split by comma (-1): %v\n", parts1) // Output: Split by comma (-1): [Apple Banana Cherry  Date]
// 注意:连续的逗号会产生空的字符串""

// 按逗号分割,限制数量 (n=3)
parts2 := reComma.Split(csvData, 3)
fmt.Printf("Split by comma (3): %v\n", parts2) // Output: Split by comma (3): [Apple Banana Cherry,,Date]
// 最后一个元素包含了剩余未分割的部分

// 按一个或多个空白字符或逗号分割
patternWhitespaceOrComma := `[\s,]+`
reWhitespaceOrComma := regexp.MustCompile(patternWhitespaceOrComma)
text := "word1   word2,word3 \nword4"
parts3 := reWhitespaceOrComma.Split(text, -1)
fmt.Printf("Split by whitespace or comma (-1): %v\n", parts3) // Output: Split by whitespace or comma (-1): [word1 word2 word3 word4]

}
“`

8. 正则表达式语法回顾 (RE2 子集)

虽然 RE2 的完整语法文档可以在 Google 的 RE2 项目页面找到,但为了方便,这里回顾一些 Go 的 regexp 包支持的常用基本语法元素:

  • 字符匹配:

    • c: 匹配字符 c 本身 (除非是元字符)。
    • .: 匹配除换行符(\n)外的任意单个字符。
    • \c: 转义字符 c。例如 \. 匹配字面量点,\\ 匹配字面量反斜杠。
    • [abc]: 字符集,匹配括号中的任意一个字符 (a, b, 或 c)。
    • [a-z]: 字符范围,匹配从 az 的任意小写字母。
    • [a-zA-Z0-9]: 匹配任意字母或数字。
    • [^abc]: 否定字符集,匹配除括号中列出字符外的任意单个字符。
    • \d: 匹配任意数字 (等同于 [0-9])。
    • \D: 匹配任意非数字 (等同于 [^0-9])。
    • \w: 匹配任意字母、数字或下划线 (word characters, 等同于 [a-zA-Z0-9_])。
    • \W: 匹配任意非字母、数字或下划线 (等同于 [^a-zA-Z0-9_])。
    • \s: 匹配任意空白字符 (空格、制表符、换行符等,等同于 [\t\n\v\f\r ])。
    • \S: 匹配任意非空白字符 (等同于 [^\t\n\v\f\r ])。
    • \p{屬性} / \P{屬性}: Unicode 属性匹配。例如 \p{Han} 匹配汉字,\p{L} 匹配任意字母,\p{N} 匹配任意数字。
    • \pN / \PN: 更常用的 Unicode 属性缩写,例如 \pL (字母), \pP (标点), \pS (符号), \pZ (分隔符)。
  • 锚点 (Anchors):

    • ^: 匹配字符串的开头(除非设置多行模式,Go 默认不开启)。
    • $: 匹配字符串的结尾(除非设置多行模式,Go 默认不开启)。
    • \A: 匹配字符串的绝对开头。
    • \Z: 匹配字符串的绝对结尾。
    • \b: 匹配单词边界 (即单词字符 \w 和非单词字符 \W 之间的位置,或字符串的开始/结束与单词字符之间的位置)。
    • \B: 匹配非单词边界。
  • 量词 (Quantifiers): 用于指定前面元素的出现次数。

    • ?: 匹配零次或一次 (等同于 {0,1})。
    • *: 匹配零次或多次 (等同于 {0,})。
    • +: 匹配一次或多次 (等同于 {1,})。
    • {n}: 匹配恰好 n 次。
    • {n,}: 匹配至少 n 次。
    • {n,m}: 匹配至少 n 次,但不超过 m 次。
  • 贪婪与非贪婪量词 (Greedy vs. Non-Greedy):
    默认情况下,量词是贪婪的,会尽可能多地匹配。在量词后面加上 ? 可以使其变为非贪婪(或惰性)模式,会尽可能少地匹配。

    • ??: 匹配零次或一次 (非贪婪)。
    • *?: 匹配零次或多次 (非贪婪)。
    • +?: 匹配一次或多次 (非贪婪)。
    • {n,}?: 匹配至少 n 次 (非贪婪)。
    • {n,m}?: 匹配至少 n 次,但不超过 m 次 (非贪婪)。

    示例 9:贪婪与非贪婪
    “`go
    package main

    import (
    “fmt”
    “regexp”
    )

    func main() {
    html := “

    Hello

    // 贪婪模式: 匹配 <.*> 会从第一个 < 匹配到最后一个 >
    reGreedy := regexp.MustCompile(`<.*>`)
    matchGreedy := reGreedy.FindString(html)
    fmt.Printf("Greedy match: '%s'\n", matchGreedy) // Output: Greedy match: '<p><b>Hello</b></p>'
    
    // 非贪婪模式: 匹配 <.*?> 会从第一个 < 匹配到遇到的第一个 >
    reNonGreedy := regexp.MustCompile(`<.*?>`)
    matchNonGreedy := reNonGreedy.FindAllString(html, -1)
    fmt.Printf("Non-greedy matches: %v\n", matchNonGreedy) // Output: Non-greedy matches: [<p> <b> </b> </p>]
    

    }
    “`

  • 分组 (Grouping) 与捕获 (Capturing):

    • (pattern): 捕获组,匹配 pattern 并捕获匹配到的文本。
    • (?:pattern): 非捕获组,匹配 pattern 但不捕获文本。
  • 分支 (Alternation):

    • a|b: 匹配 ab
  • 标志 (Flags): Go 的 regexp 包主要通过在模式开头添加 (?flags) 或在特定组上添加 (?flags:pattern) 来设置标志,而不是通过独立的函数参数。常用的标志包括:

    • i: Case-insensitive (忽略大小写)。例如 (?i)abc 匹配 “abc”, “Abc”, “aBc”, “ABC” 等。
    • m: Multi-line mode (多行模式)。^$ 匹配每一行的开始和结束,而不仅仅是整个字符串的开始和结束。
    • s: Dotall mode (点号匹配所有)。. 匹配包括换行符在内的所有字符。
    • U: Ungreedy (非贪婪)。使所有量词默认变为非贪婪。

    示例 10:使用标志
    “`go
    package main

    import (
    “fmt”
    “regexp”
    )

    func main() {
    text := “GoLang\ngolang”

    // 默认模式 (区分大小写,.不匹配换行符)
    reDefault := regexp.MustCompile(`go.lang`)
    fmt.Printf("Default match: %v\n", reDefault.FindAllString(text, -1)) // Output: Default match: [golang]
    
    // 忽略大小写标志 (?i)
    reCaseInsensitive := regexp.MustCompile(`(?i)go.lang`)
    fmt.Printf("Case-insensitive match: %v\n", reCaseInsensitive.FindAllString(text, -1)) // Output: Case-insensitive match: [GoLang golang]
    
    // 多行模式 (?m): 让 ^ 和 $ 匹配每行的开头和结尾
    // 默认情况下 ^ 和 $ 只匹配整个字符串的开头和结尾
    // 考虑文本 "Line1\nLine2"
    // ^Line1$ 默认不匹配
    // (?m)^Line1$ 匹配
    // 为了演示,我们匹配以Go开头的行
    reMultiLine := regexp.MustCompile(`(?m)^Go`)
    fmt.Printf("Multi-line match: %v\n", reMultiLine.FindAllString(text, -1)) // Output: Multi-line match: [Go]
    
    // 点号匹配所有标志 (?s): 让 . 匹配包括换行符
    reDotAll := regexp.MustCompile(`(?s)Go.*lang`) // 匹配 Go 后面跟着任意字符(包括换行)直到 lang
    fmt.Printf("Dotall match: %v\n", reDotAll.FindAllString(text, -1)) // Output: Dotall match: [GoLang
                                                                     // golang]
    

    }
    “`

  • 原始字符串字面量 (Raw String Literals):
    在 Go 中,反斜杠 \ 是转义字符。在正则表达式中,反斜杠也非常常用(例如 \d, \s, \., \\)。如果使用普通字符串字面量(双引号 ""),你需要对反斜杠进行双重转义,例如模式 \d+ 需要写成 "\\d+"。使用原始字符串字面量(反引号 `),则不需要进行双义,所见即所得。这使得正则表达式模式更易读。

    示例 11:原始字符串字面量
    “`go
    package main

    import (
    “fmt”
    “regexp”
    )

    func main() {
    // 普通字符串字面量,需要双重转义
    pattern1 := “\d+”
    re1 := regexp.MustCompile(pattern1)
    fmt.Printf(“Using escaped string: %v\n”, re1.FindAllString(“abc 123 def 456”, -1)) // Output: Using escaped string: [123 456]

    // 原始字符串字面量,无需双重转义
    pattern2 := `\d+`
    re2 := regexp.MustCompile(pattern2)
    fmt.Printf("Using raw string: %v\n", re2.FindAllString("abc 123 def 456", -1)) // Output: Using raw string: [123 456]
    
    // 原始字符串字面量处理反斜杠本身
    pattern3 := `\\` // 匹配字面量反斜杠
    re3 := regexp.MustCompile(pattern3)
    fmt.Printf("Matching backslash: %v\n", re3.FindAllString("a\\b\\c", -1)) // Output: Matching backslash: [\\ \\]
    

    }
    `
    强烈推荐在写正则表达式模式时使用原始字符串字面量,除非模式中需要包含反引号本身(这极少见,此时可以使用普通字符串并转义反引号 `\
    )。

9. 字节切片 (Bytes) 的操作

regexp 包中的大多数方法都有对应的处理字节切片 ([]byte) 的版本,名称通常去掉 String 后缀,例如 Match, Find, FindAll, ReplaceAll, Split。在处理大量二进制数据或需要极致性能时,直接使用字节切片操作可以避免字符串和字节切片之间的转换开销。

“`go
package main

import (
“fmt”
“regexp”
)

func main() {
data := []byte(“hello 123 world”)
pattern := \d+

re := regexp.MustCompile(pattern)

// 匹配字节切片
matched := re.Match(data)
fmt.Printf("Bytes match: %v\n", matched) // Output: Bytes match: true

// 查找字节切片
found := re.Find(data) // Returns []byte
fmt.Printf("Bytes find first: %s\n", found) // Output: Bytes find first: 123

// 替换字节切片
replaced := re.ReplaceAll(data, []byte("XXX"))
fmt.Printf("Bytes replace all: %s\n", replaced) // Output: Bytes replace all: hello XXX world

}
“`

10. 性能考量

  • 编译: 前面已经强调过,如果同一个正则表达式模式需要使用多次,务必先使用 regexp.Compileregexp.MustCompile 进行编译,然后使用编译后的 *regexp.Regexp 对象进行操作。避免在循环中重复调用 MatchString 或其他直接接收模式字符串的函数。
  • MustCompile vs Compile: MustCompile 在模式无效时会 panic。如果你的模式是硬编码在代码中的常量,并且你在开发时已经测试过它是有效的,那么在包级别的初始化中使用 MustCompile 是一个常见的实践。例如:
    go
    var emailRegex = regexp.MustCompile(`^[^@\s]+@[^@\s]+\.[^@\s]+$`) // Package level variable
    func IsValidEmail(email string) bool {
    return emailRegex.MatchString(email) // Reuse the compiled regex
    }

    如果正则表达式模式来自用户输入或外部配置,你应该使用 Compile,并在编译失败时处理返回的错误。
  • 简单操作优先使用 strings 包: 对于简单的字符串查找、判断前缀/后缀、分割固定字符等操作,strings 包通常比 regexp 包更快速、更高效。正则表达式引擎有其自身的开销。例如,检查字符串是否包含子串,使用 strings.Containsregexp.MatchString(".*substr.*", s)regexp.MustCompile("substr").MatchString(s) 要快得多。
  • RE2 的优势: Go 的 regexp 包基于 RE2,它保证了匹配时间的线性性,即使模式很复杂或输入很糟糕,也不会出现灾难性回溯导致的性能急剧下降。这在处理不可信的外部输入时是一个重要的安全特性。
  • 避免不必要的捕获: 如果不需要捕获组的内容,使用非捕获组 (?:...) 可以略微提高性能。

11. 常见陷阱与注意事项

  • 反斜杠转义: 使用原始字符串字面量 `...` 可以极大地简化模式书写,避免复杂的反斜杠转义。
  • MustCompile 的 Panic: 如前所述,仅在你确定正则表达式模式在编译时是有效的情况下使用 MustCompile。否则,使用 Compile 并检查错误。
  • 多行模式和锚点: 默认情况下,^$ 匹配整个字符串的开始和结束。如果你需要它们匹配每一行的开始和结束,请在模式中包含 (?m) 标志。
  • 点号匹配换行: 默认情况下,. 不匹配换行符 \n。如果需要匹配包括换行符在内的任意字符,请在模式中包含 (?s) 标志。
  • 复杂模式的调试: 复杂的正则表达式很难一次写对。可以使用在线的正则表达式测试工具(例如 regex101.com, regexr.com)来构建和测试你的模式,理解每部分是如何工作的,以及如何匹配和捕获。
  • Unicode 支持: Go 的 regexp 包对 Unicode 有良好的支持,可以使用 \p{...} 属性类来匹配各种 Unicode 字符类别。

12. 实际应用示例

示例 12:简单的 URL 匹配和提取

“`go
package main

import (
“fmt”
“regexp”
)

func main() {
text := Visit our website at https://www.example.com or http://sub.example.org/path.
// 匹配 http 或 https 开头
// ://
// 非空白字符 (域名+路径)
pattern := (https?://[^\s]+)

re := regexp.MustCompile(pattern)

// 查找所有 URL
urls := re.FindAllString(text, -1)
fmt.Println("Found URLs:", urls) // Output: Found URLs: [https://www.example.com http://sub.example.org/path.]

// 查找第一个 URL 的域名部分 (稍微复杂一点)
// 匹配 https?://
// ([^/\s]+) 捕获域名部分(非 / 和 非空白字符)
// 可选的 / 及其后的任意非空白字符 (路径部分,不捕获)
patternDomain := `https?://([^/\s]+)(?:/[\S]*)?` // Non-capturing group for path
reDomain := regexp.MustCompile(patternDomain)

firstMatchAndDomain := reDomain.FindStringSubmatch(text)
if len(firstMatchAndDomain) > 1 {
    fmt.Printf("First URL: %s, Domain: %s\n", firstMatchAndDomain[0], firstMatchAndDomain[1]) // Output: First URL: https://www.example.com, Domain: www.example.com
}

allMatchesAndDomains := reDomain.FindAllStringSubmatch(text, -1)
fmt.Println("\nAll URL and Domain pairs:")
for _, match := range allMatchesAndDomains {
    if len(match) > 1 {
        fmt.Printf("  URL: %s, Domain: %s\n", match[0], match[1])
    }
}
/* Output:
All URL and Domain pairs:
  URL: https://www.example.com, Domain: www.example.com
  URL: http://sub.example.org/path., Domain: sub.example.org
*/

}
``
请注意,实际的 URL 匹配和解析通常应该使用 Go 标准库中的
net/url` 包,因为它能更好地处理各种 URL 的复杂情况和边缘情况,而不是仅仅依赖于正则表达式。正则表达式适用于简单的模式查找和提取,但对于结构化数据的解析,通常有更健壮的专用库。

13. regexp/syntax

Go 标准库中还有一个 regexp/syntax 包,它提供了正则表达式的解析和抽象语法树 (AST) 表示。如果你需要分析、转换或检查正则表达式本身的结构(而不是执行匹配),这个包会很有用。普通用户通常不需要直接使用它。

总结

Go 语言的 regexp 包提供了一个强大且高效的正则表达式引擎,基于 RE2 实现,保证了线性时间的匹配性能。通过编译正则表达式、使用原始字符串字面量以及掌握基本的匹配、查找、替换、分割和捕获组操作,你可以有效地处理各种文本模式匹配任务。

掌握正则表达式是一项非常有价值的技能,它可以显著提高你在处理文本数据时的效率。结合 Go 语言简洁的语法和 regexp 包提供的丰富功能,你将能够轻松应对复杂的文本处理挑战。记住在频繁使用同一模式时进行编译,并在处理简单任务时优先考虑 strings 包,以充分发挥 Go 的性能优势。

多实践、多尝试不同的模式,是掌握正则表达式的关键。希望这篇文章为你提供了 Go 语言中正则表达式的全面入门指南。


发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部