📌  相关文章
📜  在 GoLang 中使用正则表达式进行匹配

📅  最后修改于: 2021-10-24 14:03:02             🧑  作者: Mango

Regexp 是正则表达式的缩写形式。人们通常将 regexp 与 regex 混淆,这很明显,因为其他编程语言(例如Python)使用术语 regex 作为其正则表达式库,但 Go 使用regexp ,重要的是要记住它作为 regexp,因为它是可用的正则表达式包的名称在 Go 中使用预先构建的函数。

匹配——是什么?

好吧,不管出身如何,每个孩子在他们的生活中可能至少经历过一种情况,他们可能穿着相配的衣服或相配的配饰,然后说,“嘿,伙计,我们穿了相配的衣服”,“同样的捏”等等。这些孩子究竟是如何决定某个东西是否与另一个匹配的?这很简单!当两个事物完全相同,或者一个事物的一部分与其他事物完全相同时,我们说这两个事物分别是匹配或子匹配。因此,让我们更深入地了解 GoLang 中的匹配概念。

在 GoLang 中使用正则表达式进行匹配

regexp(正则表达式)是关于字符串/模式匹配的。每个函数,正则表达式函数的每个部分都需要文本匹配。让我们看看一些对于直接或间接匹配很重要的函数。

请参阅表 1.1以了解有关用于编译和存储正则表达式对象的方法的更多信息。有关直接实现 Match 接口的函数的更多信息,请参阅表 1.2参见表 1.3 。了解更多有关执行查找、替换等操作的 regexp 的其他预构建方法的更多信息。这些方法首先间接实现 Match 接口,然后执行它们的操作。

你怀疑他们是怎么做到的吗?好的,考虑这个例子:如果你的妈妈让你在厨房里找东西,只要提供一个物品的描述,你会怎么做?你会去厨房寻找符合你母亲描述的物品,然后你将它挑选出来交给她。这意味着您只需先匹配描述并报告给您的妈妈(尽管您的主要重点是找到它,但您必须间接匹配才能找到它)。知道了?现在让我们更深入地了解这些函数。

COMPILE

Function Description

regexp.Compile( ) This method creates a regex object that can match the text provided in the input in case no errors occur. If any error occurs then this method returns an error. This method returns a regex object and an error message (if any).
regexp.CompilePOSIX( ) It does the same as Compile( ) but the difference is that it restricts the text to POSIX format.
regexp.MustCompile( ) This method creates a regex object that can match the text provided in the input in case no errors occur. The main difference  between Compile( ) and MustCompile( ) is that in case of errors, Compile( ) simply returns an error message to the second err variable but MustCompile( ) panics and raises an issue. Also, MustCompile( ) returns only the object to match or panics, but not a second variable.
regexp.MustCompilePOSIX( ) It does the same as MustCompile( ) but the difference is that it restricts the text to POSIX format.

表 1.1。编译方法

DIRECT MATCH FUNCTIONS

Function Description

regexp.Match( )

The Match( ) takes a byte array as input and checks whether the byte array text matches the one held by regex object that called the Match( ). If it matches, then the method returns a boolean value: true or else a boolean value: false.

regexp.MatchString( )

Works the same as Match( ). The main difference is that it collects a string type text as an input and returns the boolean value according to the respective match/mismatch.

regexp.MatchReader( )

Works the same as Match( ). The main difference is that it collects a rune of reader type as an input and returns the boolean value according to the respective match/mismatch.

表 1.2。实现 Match 接口的直接匹配函数。

FIND FUNCTIONS

Function Description

regexp.Find( ) Returns a byte array/slice of the first occurrence of the input text with regex object text.
regexp.FindString( ) Returns a string of the first occurrence of the input text with regex object text.
regexp.FindSubmatch( ) Returns a byte array/slice of the first occurrence of any of the subset-match of input text with regex object text.
regexp.FindStringSubmatch( ) Returns a string of the first occurrence of any of the subset-match of input text with regex object text.
regexp.FindAll( ) Returns a byte array of all byte arrays/slices of all occurrences of the input text with regex object text.
regexp.FindAllString( ) Returns an array of all strings of all occurrences of the input text with regex object text.
regexp.FindAllSubmatch( ) Returns a byte array of all byte arrays/slices that are subset-matches of input text with regex object text.
regexp.FindAllStringSubmatch( ) Returns an array of all subset-strings of all occurrences of the input text with regex object text.
regexp.FindIndex( ) Returns the index of the first occurrence of the matched byte array/slice input text with regex object text.
regexp.FindStringIndex( ) Returns the index of the first occurrence of the matched string input text with the regex object text.
regexp.FindSubmatchIndex( ) Returns the index of the first subset-match of input slice with the respective regex object text.
regexp.FindStringSubmatchIndex( ) Returns the index of the first subset-match of the input string with the respective regex object text.
regexp.FindAllIndex( ) Returns an array of all such indices which match the input slice text with regex object text.
regexp.FindAllStringIndex( ) Returns an array of all such indices which match the input string text with regex object text.
regexp.FindAllSubmatchIndex( ) Returns an array of all such indices which match partially the input slice text with regex object text.
regexp.FindAllStringSubmatchIndex( ) Returns an array of all such indices which match partially the input string text with regex object text.
regexp.FindReaderIndex( ) Returns a slice of integers that define the location of the first complete occurrence of regex object text in the RuneReader text.
regexp.FindReaderSubmatchIndex( ) Returns a slice of integers that define the location of the first partial occurrence of regex object text in the RuneReader text.
regexp.ReplaceAll( ) As the name suggests, this function replaces all the values in input slice (arg1) with input slice (arg2) by  matching the text mentioned in regex object with that in input slice (arg1). It returns a copy of slice, modified. Also, in arg2 all ‘$’ signs are interpreted the same way as in Expand.
regexp.ReplaceAllFunc( ) Almost the same as ReplaceAll( ) with one difference that the slice in arg2 is not directly input by user but instead a function is called which returns a slice that takes arg2’s place. It returns a copy of slice, modified.
regexp.ReplaceAllString( ) Same as ReplaceAll( ) but the only difference is that in collects string arguments and also returns string copy of the slice, modified.
regexp.ReplaceAllLiteral( ) As the name suggests, this function replaces all the values in input slice (arg1) with input slice (arg2) by  matching the text mentioned in regex object with that in input slice (arg1). And literally the text in arg2 is exactly considered unlike ReplaceAll( ) that has a different convention for some signs like for instance, ‘$’.  It returns the modified slice.
regexp.ReplaceAllStringFunc( ) Same as ReplaceAllFunc( ) but the only difference is that this function operates with string inputs & string output.
regexp.ReplaceAllLiteralString( ) Same as ReplaceAllLiteral( ) but the only difference is that this function operates with string inputs & string output.

表 1.3。其他执行不同操作但为各自的操作间接实现Match接口的函数。

笔记: 我们一直在提到____的副本,已修改和未修改___。这是因为更改不是直接对源字符串执行的。相反,默认情况下会传递一个副本,并对副本执行修改。

正如我们之前提到的,regexp 本身是一组处理字符串匹配的操作。因此,regexp 包中的几乎每个函数都直接或间接地处理匹配。无需惊慌,因为所有这些操作都是完全实时且易于理解的。

例子:

代码 1:直接实现 Match 方法

Go
package main
  
import (
    f "fmt"
    "regexp"
)
  
func main() {
    f.Println("--------Reference strings--------\n")
  
    name := "My name is Geeks for geeks."
    f.Println(name)
  
    profession := "I am a computer science portal for geeks."
    f.Println(profession)
  
    message := "You can find anything here, if not tell us and we'll add it for you!"
    f.Println(message)
  
    //---------------------------------------------------
    f.Println("\n--------Match functions--------")
    //-------------------------------------------
  
    obj, err := regexp.Match("[gG]e*k.*", []byte(name))
    f.Println("\nregex.Match returns ->", obj,
        "and error(if any) --->", err)
  
    //-------------------------------------------
    obj, err = regexp.Match("[gG]e*k.*", []byte(profession))
    f.Println("\nregex.Match returns ->", obj,
        "and error(if any) --->", err)
  
    //-------------------------------------------
    obj, err = regexp.MatchString("Geek.*", message)
    f.Println("\nregex.MatchString returns ->", obj,
        "and error(if any) --->", err)
  
    //-------------------------------------------
}


Go
package main
  
import (
    f "fmt"
    "io"
    "regexp"
)
  
func main() {
    obj := regexp.MustCompile("ee")
    var r io.RuneReader
    s := []byte("Hello GeekS, 1234")
    f.Println("Initial byte array -----> ", s)
    f.Println("Initial string ---------> ", string(s))
    f.Println("MatchReader ------------> ", obj.MatchReader(r))
    ex := []byte("NEW")
    f.Println("ReplaceAllFunc( ) work in progress...")
    s = obj.ReplaceAllFunc(s, func(s []byte) []byte {
        if true {
            return ex
        }
        return s
    })
    f.Println("Final string -----------> ", string(s))
    f.Println("Final byte array -------> ", s)
}


Go
package main
  
import (
    f "fmt"
    "regexp"
)
  
func main() {
    f.Println("--------Reference strings--------\n")
  
    name := "My name is Geeks for geeks."
    f.Println(name)
  
    profession := "I am a computer science portal for geeks."
    f.Println(profession)
  
    message := "You can find anything here, if not tell us and we'll add it for you!"
    f.Println(message)
    //---------------------------------------------------------
    f.Println("\n--------Compiling functions--------\n")
    //-------------------------------------------
  
    musComp := regexp.MustCompile("[gG]ee.?")
    f.Println("Initialized the regexp object to musComp...")
  
    //---------------------------------------------------------
    f.Println("\n--------Find functions--------\n")
    //-------------------------------------------
  
    f.Println("mustCompile.Find -----------------------> ",
        musComp.Find([]byte(name)))
  
    f.Println("mustCompile.FindString -----------------> ",
        musComp.FindString(name))
  
    f.Println("mustCompile.FindSubmatch ---------------> ",
        musComp.FindSubmatch([]byte(name)))
  
    f.Println("mustCompile.FindStringSubmatch ---------> ",
        musComp.FindStringSubmatch(name))
    //-------------------------------------------
    f.Println("mustCompile.FindAll --------------------> ",
        musComp.FindAll([]byte(name), -1))
  
    f.Println("mustCompile.FindAllString --------------> ",
        musComp.FindAllString(name, -1))
  
    f.Println("mustCompile.FindAllSubmatch ------------> ",
        musComp.FindAllSubmatch([]byte(name), -1))
  
    f.Println("mustCompile.FindAllStringSubmatch ------> ",
        musComp.FindAllStringSubmatch(name, -1))
    //-------------------------------------------
    f.Println("mustCompile.FindIndex ------------------> ",
        musComp.FindIndex([]byte(name)))
  
    f.Println("mustCompile.FindStringIndex ------------> ",
        musComp.FindStringIndex(name))
  
    f.Println("mustCompile.FindSubmatchIndex ----------> ",
        musComp.FindSubmatchIndex([]byte(name)))
  
    f.Println("mustCompile.FindStringSubmatchIndex ----> ",
        musComp.FindStringSubmatchIndex(name))
    //-------------------------------------------
    f.Println("mustCompile.FindAllIndex ---------------> ",
        musComp.FindAllIndex([]byte(name), -1))
  
    f.Println("mustCompile.FindAllStringIndex ---------> ",
        musComp.FindAllStringIndex(name, -1))
  
    f.Println("mustCompile.FindAllSubmatchIndex -------> ",
        musComp.FindAllSubmatchIndex([]byte(name), -1))
  
    f.Println("mustCompile.FindAllStringSubmatchIndex -> ",
        musComp.FindAllStringSubmatchIndex(name, -1))
    //------------------------------------------------------------
    f.Println("\n--------Replace functions--------\n")
    //-------------------------------------------
  
    f.Println("mustCompile.ReplaceAll -----------------> ",
        musComp.ReplaceAll([]byte(name), []byte("Bow bow!")))
  
    f.Println("mustCompile.ReplaceAllStirng -----------> ",
        musComp.ReplaceAllString(name, "Bow bow!"))
  
    f.Println("mustCompile.ReplaceAllLiteral ----------> ",
        musComp.ReplaceAllLiteral([]byte(name), []byte("T")))
  
    f.Println("mustCompile.ReplaceAllLiteralString ----> ",
        musComp.ReplaceAllLiteralString(name, "T"))
    //------------------------------------------------------------
}


在命令提示符下运行的命令:

:/Directory where the go file is present/> go run (file_name).go

输出:

--------Reference strings--------

My name is Geeks for geeks.
I am a computer science portal for geeks.
You can find anything here, if not tell us and we'll add it for you!

--------Match functions--------

regex.Match returns -> true and error(if any) ---> 

regex.Match returns -> true and error(if any) ---> 

regex.MatchString returns -> false and error(if any) ---> 

代码 2:直接实现 Match 方法

package main
  
import (
    f "fmt"
    "io"
    "regexp"
)
  
func main() {
    obj := regexp.MustCompile("ee")
    var r io.RuneReader
    s := []byte("Hello GeekS, 1234")
    f.Println("Initial byte array -----> ", s)
    f.Println("Initial string ---------> ", string(s))
    f.Println("MatchReader ------------> ", obj.MatchReader(r))
    ex := []byte("NEW")
    f.Println("ReplaceAllFunc( ) work in progress...")
    s = obj.ReplaceAllFunc(s, func(s []byte) []byte {
        if true {
            return ex
        }
        return s
    })
    f.Println("Final string -----------> ", string(s))
    f.Println("Final byte array -------> ", s)
}

在命令提示符下运行的命令:

:/Directory where the go file is present/> go run (file_name).go

输出:

代码 3:Match 方法的间接实现

package main
  
import (
    f "fmt"
    "regexp"
)
  
func main() {
    f.Println("--------Reference strings--------\n")
  
    name := "My name is Geeks for geeks."
    f.Println(name)
  
    profession := "I am a computer science portal for geeks."
    f.Println(profession)
  
    message := "You can find anything here, if not tell us and we'll add it for you!"
    f.Println(message)
    //---------------------------------------------------------
    f.Println("\n--------Compiling functions--------\n")
    //-------------------------------------------
  
    musComp := regexp.MustCompile("[gG]ee.?")
    f.Println("Initialized the regexp object to musComp...")
  
    //---------------------------------------------------------
    f.Println("\n--------Find functions--------\n")
    //-------------------------------------------
  
    f.Println("mustCompile.Find -----------------------> ",
        musComp.Find([]byte(name)))
  
    f.Println("mustCompile.FindString -----------------> ",
        musComp.FindString(name))
  
    f.Println("mustCompile.FindSubmatch ---------------> ",
        musComp.FindSubmatch([]byte(name)))
  
    f.Println("mustCompile.FindStringSubmatch ---------> ",
        musComp.FindStringSubmatch(name))
    //-------------------------------------------
    f.Println("mustCompile.FindAll --------------------> ",
        musComp.FindAll([]byte(name), -1))
  
    f.Println("mustCompile.FindAllString --------------> ",
        musComp.FindAllString(name, -1))
  
    f.Println("mustCompile.FindAllSubmatch ------------> ",
        musComp.FindAllSubmatch([]byte(name), -1))
  
    f.Println("mustCompile.FindAllStringSubmatch ------> ",
        musComp.FindAllStringSubmatch(name, -1))
    //-------------------------------------------
    f.Println("mustCompile.FindIndex ------------------> ",
        musComp.FindIndex([]byte(name)))
  
    f.Println("mustCompile.FindStringIndex ------------> ",
        musComp.FindStringIndex(name))
  
    f.Println("mustCompile.FindSubmatchIndex ----------> ",
        musComp.FindSubmatchIndex([]byte(name)))
  
    f.Println("mustCompile.FindStringSubmatchIndex ----> ",
        musComp.FindStringSubmatchIndex(name))
    //-------------------------------------------
    f.Println("mustCompile.FindAllIndex ---------------> ",
        musComp.FindAllIndex([]byte(name), -1))
  
    f.Println("mustCompile.FindAllStringIndex ---------> ",
        musComp.FindAllStringIndex(name, -1))
  
    f.Println("mustCompile.FindAllSubmatchIndex -------> ",
        musComp.FindAllSubmatchIndex([]byte(name), -1))
  
    f.Println("mustCompile.FindAllStringSubmatchIndex -> ",
        musComp.FindAllStringSubmatchIndex(name, -1))
    //------------------------------------------------------------
    f.Println("\n--------Replace functions--------\n")
    //-------------------------------------------
  
    f.Println("mustCompile.ReplaceAll -----------------> ",
        musComp.ReplaceAll([]byte(name), []byte("Bow bow!")))
  
    f.Println("mustCompile.ReplaceAllStirng -----------> ",
        musComp.ReplaceAllString(name, "Bow bow!"))
  
    f.Println("mustCompile.ReplaceAllLiteral ----------> ",
        musComp.ReplaceAllLiteral([]byte(name), []byte("T")))
  
    f.Println("mustCompile.ReplaceAllLiteralString ----> ",
        musComp.ReplaceAllLiteralString(name, "T"))
    //------------------------------------------------------------
}

在命令提示符下运行的命令:

:/Directory where the go file is present/> go run (file_name).go

输出: