📜  Golang 中的正则表达式是什么?

📅  最后修改于: 2021-10-24 13:15:44             🧑  作者: Mango

正则表达式(或 RegEx)是一个特殊的字符序列,它定义了用于匹配特定文本的搜索模式。在 Golang 中,有一个用于正则表达式的内置包,称为regexp包,其中包含所有操作列表,例如过滤、替换、验证或提取。它使用 RE2 语法标准。 MatchString()函数报告作为参数传递的字符串是否包含正则表达式模式的任何匹配项。

句法:

func MatchString(pattern string, s string)

返回:匹配的布尔值,错误错误

例子:

// Golang program to illustrate the
// string matching using regexp
// in-built function
package main
  
import (
    "fmt"
    "regexp"
)
  
func main() {
  
    // string in which the pattern
    // is to be found
    str := "geeksforgeeks"
  
    // returns true if the pattern is present
    // in the string, else returns false
    // err is nil if the regexp is valid
    match1, err := regexp.MatchString("geeks", str)
    fmt.Println("Match: ", match1, " Error: ", err)
  
    // this returns false as the match
    // is unsuccessful
    str2 := "ComputerScience"
    match2, err := regexp.MatchString("geeks", str2)
    fmt.Println("Match: ", match2, "Error: ", err)
  
    // this will throw an error
    // as the pattern is not valid
    match3, err := regexp.MatchString("geek(s", str2)
    fmt.Println("Match: ", match3, "Error: ", err)
}

输出:

Match:  true  Error:  
Match:  false Error:  
Match:  false Error:  error parsing regexp: missing closing ): `geek(s`

为了存储复杂的正则表达式以供以后重用, Compile()方法解析正则表达式并在成功时返回一个 Regexp 对象,该对象可用于匹配文本。函数原型为:

func Compile(expr string) (*Regexp, error)

regexp 包中提供了其他各种方法来匹配字符串,如下所示:

// Golang program to illustrate the
// string matching using regexp
// in-built functions
package main
  
import (
    "fmt"
    "regexp"
    "strings"
)
  
func main() {
  
    // a regex object which
    // can be reused later
    re, _ := regexp.Compile("geek")
  
    // string to be matched
    str := "I love geeksforgeeks"
  
    // returns the slice of first
    // and last index
    match := re.FindStringIndex(str)
    fmt.Println(match)
  
    str2 := "I love computer science"
  
    // prints an empty slice
    // as there is no match
    match2 := re.FindStringIndex(str2)
    fmt.Println(match2)
  
    // finds the first or leftmost
    // match to a given pattern.
    re2, _ := regexp.Compile("[0-9]+-v.*g")
  
    // matches one or more numbers followed
    // by v and any number of characters upto g
    match3 := re2.FindString("20024-vani_gupta")
    fmt.Println(match3)
  
    // returns a slice of all successive
    // matches of the expression
    match4 := re.FindAllStringSubmatchIndex("I'am a geek at"+
                                        " geeksforgeeks", -1)
    fmt.Println(match4)
  
    // returns a copy and replaces
    // matches with the replacement string
    re3, _ := regexp.Compile(" ")
    match5 := re3.ReplaceAllString("All I do"+
                    " is code everytime.", "+")
    fmt.Println(match5)
  
    // returns a copy in which all matches are
    // replaced by return value of function
    re4, _ := regexp.Compile("[aeiou]+")
    match6 := re4.ReplaceAllStringFunc("All I do"+
           " is code everytime.", strings.ToUpper)
    fmt.Println(match6)
}

输出:

[7 11]
[]
20024-vani_g
[[7 11] [15 19] [23 27]]
All+I+do+is+code+everytime.
All I dO Is cOdE EvErytImE.