Matching using regexp in GoLang

14 Oct, 2020
Regexp is the short form of Regular expressions. People generally confuse regexp with regex, and that’s obvious because other programming languages such as Python use the term regex for their regular expression library, but Go uses regexp and it’s important to remember it as regexp as it is the name of the regular expressions package available in Go with pre-built functions.
Well, irrespective of the origin, every kid may have experienced at least one situation in their lives where they may have worn matching clothes or matching accessories and have gone saying, “Hey buddy we wore matching clothes”, “same pinch” etc.. How exactly did those kids decide if something matches with the other? It’s simple! When two things are exactly the same, or a part of one exactly is the same as some other thing, we say those two things are a match or a sub-match respectively. So let’s dive deeper into the concept of matching in GoLang.
regexp (regular expressions) is all about string/pattern matching. Every function, every part of regexp functions somewhere requires text matching. Let us look at some functions that are important for matching directly or indirectly. 
Refer Table 1.1 to know more about the methods used to compile and store a regex object. Refer Table 1.2 to know more about the functions that directly implement the Match interface and nothing more. Refer to Table 1.3. to know more about the other pre-built methods of regexp that perform operations such as find, replace, etc.. which indirectly implement the Match interface first and then perform their operation. 
Do you doubt how they do so? Okay consider this example: If your mother asks you to find something in the kitchen by just providing a description of the object, what will you do? You’ll go to the kitchen and then search for objects that match your mother’s description and then you pick it and give it to her. This means you simply matched the description first and reported back to your mom (though your prime focus was to just find it, you indirectly had to match in order to find it). Got it? Let’s now take a deeper look at these functions. 
COMPILE
Function Description
Table 1.1. Compilation methods
 
DIRECT MATCH FUNCTIONS
Function Description
regexp.Match( )
regexp.MatchString( )
regexp.MatchReader( )
Table 1.2. Direct match functions that implement the Match interface.
 
FIND FUNCTIONS
Function Description
Table 1.3. Other functions that perform different operations but indirectly implement the Match interface for their respective operations.
Note: We’ve been mentioning a copy of ____, modified and not modified ___.  This is so because changes are not directly performed to the source string.  Instead, a copy is passed by default, and modifications are performed on the copy.
As we mentioned earlier, regexp itself is a bundle of operations dealing with string matching. So directly or indirectly almost every function in the regexp package deals with matching. There’s no need to panic as all these operations are completely real-time and easy to understand.
Code 1: Direct implementation of Match methods
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)      //-------------------------------------------}
Command to run on command prompt:
:/Directory where the go file is present/> go run (file_name).go

Output:
--------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) ---> <nil>

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

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

Code 2: Direct implementation of the Match method(s)
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)}
Command to run on command prompt:
:/Directory where the go file is present/> go run (file_name).go

Output:
Initial byte array —–> [72 101 108 108 111 32 71 101 101 107 83 44 32 49 50 51 52]Initial string ———> Hello GeekS, 1234MatchReader ————> falseReplaceAllFunc( ) work in progress...Final string ———–> Hello GNEWkS, 1234Final byte array ——-> [72 101 108 108 111 32 71 78 69 87 107 83 44 32 49 50 51 52]
Code 3: Indirect implementation of Match methods
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"))    //------------------------------------------------------------}
Command to run on command prompt:
:/Directory where the go file is present/> go run (file_name).go

Output:
——–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!
——–Compiling functions——–
Initialized the regexp object to musComp...
——–Find functions——–
mustCompile.Find ———————–>  [71 101 101 107]mustCompile.FindString —————–>  GeekmustCompile.FindSubmatch —————>  [[71 101 101 107]]mustCompile.FindStringSubmatch ———>  [Geek]mustCompile.FindAll ——————–>  [[71 101 101 107] [103 101 101 107]]mustCompile.FindAllString ————–>  [Geek geek]mustCompile.FindAllSubmatch ————>  [[[71 101 101 107]] [[103 101 101 107]]]mustCompile.FindAllStringSubmatch ——>  [[Geek] [geek]]mustCompile.FindIndex ——————>  [11 15]mustCompile.FindStringIndex ————>  [11 15]mustCompile.FindSubmatchIndex ———->  [11 15]mustCompile.FindStringSubmatchIndex —->  [11 15]mustCompile.FindAllIndex —————>  [[11 15] [21 25]]mustCompile.FindAllStringIndex ———>  [[11 15] [21 25]]mustCompile.FindAllSubmatchIndex ——->  [[11 15] [21 25]]mustCompile.FindAllStringSubmatchIndex ->  [[11 15] [21 25]]
——–Replace functions——–
mustCompile.ReplaceAll —————–>  [77 121 32 110 97 109 101 32 105 115 32 66 111 119 32 98 111 119 33 115 32 102 111 114 32 66 111 119 32 98 111 119 33 115 46]mustCompile.ReplaceAllStirng ———–>  My name is Bow bow!s for Bow bow!s.mustCompile.ReplaceAllLiteral ———->  [77 121 32 110 97 109 101 32 105 115 32 84 115 32 102 111 114 32 84 115 46]mustCompile.ReplaceAllLiteralString —->  My name is Ts for Ts.
Golang
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Parse JSON in Golang?
How to iterate over an Array using for loop in Golang?
Structures in Golang
Time Durations in Golang
Constants- Go Language
Loops in Go Language
Strings in Golang
time.Parse() Function in Golang With Examples
Go Variables
Class and Object in Golang