Matching using regexp in GoLang
Last Updated :
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.
Matching - What is it?
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.
Matching using regexp 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
|
---|
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. |
Table 1.1. Compilation methods
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. |
Table 1.2. Direct match functions that implement the Match interface.
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. |
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.
Examples:
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, 1234
MatchReader ------------> false
ReplaceAllFunc( ) work in progress...
Final string -----------> Hello GNEWkS, 1234
Final 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 -----------------> Geek
mustCompile.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.
Similar Reads
Go Tutorial Go or you say Golang is a procedural and statically typed programming language having the syntax similar to C programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language and mainly used in Google
2 min read
Overview
Go Programming Language (Introduction)Go is a procedural programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language. Programs are assembled by using packages, for efficient management of dependencies. This language also supports env
11 min read
How to Install Go on Windows?Prerequisite: Introduction to Go Programming Language Before, we start with the process of Installing Golang on our System. We must have first-hand knowledge of What the Go Language is and what it actually does? Go is an open-source and statically typed programming language developed in 2007 by Robe
3 min read
How to Install Golang on MacOS?Before, we start with the process of Installing Golang on our System. We must have first-hand knowledge of What the Go Language is and what it actually does? Go is an open-source and statically typed programming language developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but
4 min read
Hello World in GolangHello, World! is the first basic program in any programming language. Letâs write the first program in the Go Language using the following steps:First of all open Go compiler. In Go language, the program is saved with .go extension and it is a UTF-8 text file.Now, first add the package main in your
3 min read
Fundamentals
Identifiers in Go LanguageIn programming languages, identifiers are used for identification purposes. In other words, identifiers are the user-defined names of the program components. In the Go language, an identifier can be a variable name, function name, constant, statement label, package name, or type. Example: package ma
3 min read
Go KeywordsKeywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to use as an identifier. Doing this will result in a compile-time error. Example: C // Go program to illustrate the // use of key
2 min read
Data Types in GoData types specify the type of data that a valid Go variable can hold. In Go language, the type is divided into four categories which are as follows: Basic type: Numbers, strings, and booleans come under this category.Aggregate type: Array and structs come under this category.Reference type: Pointer
7 min read
Go VariablesA typical program uses various values that may change during its execution. For Example, a program that performs some operations on the values entered by the user. The values entered by one user may differ from those entered by another user. Hence this makes it necessary to use variables as another
9 min read
Constants- Go LanguageAs the name CONSTANTS suggests, it means fixed. In programming languages also it is same i.e., once the value of constant is defined, it cannot be modified further. There can be any basic data type of constants like an integer constant, a floating constant, a character constant, or a string literal.
6 min read
Go OperatorsOperators are the foundation of any programming language. Thus the functionality of the Go language is incomplete without the use of operators. Operators allow us to perform different kinds of operations on operands. In the Go language, operators Can be categorized based on their different functiona
9 min read
Control Statements
Functions & Methods
Functions in Go LanguageIn Go, functions are blocks of code that perform specific tasks, which can be reused throughout the program to save memory, improve readability, and save time. Functions may or may not return a value to the caller.Example:Gopackage main import "fmt" // multiply() multiplies two integers and returns
3 min read
Variadic Functions in GoVariadic functions in Go allow you to pass a variable number of arguments to a function. This feature is useful when you donât know beforehand how many arguments you will pass. A variadic function accepts multiple arguments of the same type and can be called with any number of arguments, including n
3 min read
Anonymous function in Go LanguageAn anonymous function is a function that doesnât have a name. It is useful when you want to create an inline function. In Go, an anonymous function can also form a closure. An anonymous function is also known as a function literal.ExampleGopackage main import "fmt" func main() { // Anonymous functio
2 min read
main and init function in GolangThe Go language reserve two functions for special purpose and the functions are main() and init() function.main() functionIn Go language, the main package is a special package which is used with the programs that are executable and this package contains main() function. The main() function is a spec
2 min read
What is Blank Identifier(underscore) in Golang?_(underscore) in Golang is known as the Blank Identifier. Identifiers are the user-defined name of the program components used for the identification purpose. Golang has a special feature to define and use the unused variable using Blank Identifier. Unused variables are those variables that are defi
3 min read
Defer Keyword in GolangIn Go language, defer statements delay the execution of the function or method or an anonymous method until the nearby functions returns. In other words, defer function or method call arguments evaluate instantly, but they don't execute until the nearby functions returns. You can create a deferred m
3 min read
Methods in GolangGo methods are like functions but with a key difference: they have a receiver argument, which allows access to the receiver's properties. The receiver can be a struct or non-struct type, but both must be in the same package. Methods cannot be created for types defined in other packages, including bu
3 min read
Structure
Arrays
Slices
Slices in GolangSlices in Go are a flexible and efficient way to represent arrays, and they are often used in place of arrays because of their dynamic size and added features. A slice is a reference to a portion of an array. It's a data structure that describes a portion of an array by specifying the starting index
14 min read
Slice Composite Literal in GoThere are two terms i.e. Slice and Composite Literal. Slice is a composite data type similar to an array which is used to hold the elements of the same data type. The main difference between array and slice is that slice can vary in size dynamically but not an array. Composite literals are used to c
3 min read
How to sort a slice of ints in Golang?In Go, slices provide a flexible way to manage sequences of elements. To sort a slice of ints, the sort package offers a few straightforward functions. In this article we will learn How to Sort a Slice of Ints in Golang.ExampleGopackage main import ( "fmt" "sort" ) func main() { intSlice := []int{42
2 min read
How to trim a slice of bytes in Golang?In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In the Go slice of bytes, you ar
3 min read
How to split a slice of bytes in Golang?In Golang, you can split a slice of bytes into multiple parts using the bytes.Split function. This is useful when dealing with data like encoded strings, file contents, or byte streams that must be divided by a specific delimiter.Examplepackage mainimport ( "bytes" "fmt")func main() { // Initial byt
3 min read
Strings
Strings in GolangIn the Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where every character is represented by one or more bytes using UTF-8 Encoding. In other words, strings are the immutable chain of arbitrary bytes(including bytes
7 min read
How to Trim a String in Golang?In Go, strings are UTF-8 encoded sequences of variable-width characters, unlike some other languages like Java, python and C++. Go provides several functions within the strings package to trim characters from strings.In this article we will learn how to Trim a String in Golang.Examples := "@@Hello,
2 min read
How to Split a String in Golang?In Go language, strings differ from other languages like Java, C++, and Python. A string in Go is a sequence of variable-width characters, with each character represented by one or more bytes using UTF-8 encoding. In Go, you can split a string into a slice using several functions provided in the str
3 min read
Different ways to compare Strings in GolangIn Go, strings are immutable sequences of bytes encoded in UTF-8. You can compare them using comparison operators or the strings.Compare function. In this article,we will learn different ways to compare Strings in Golang.Examplepackage main import ( "fmt" "strings" ) func main() { s1 := "Hello" s2 :
2 min read
Pointers