Updated April 15, 2023
Introduction to Golang RegEx
The following article provides an outline for Golang RegEx. In go language regex is one expression which allow us to handle matching of combination of various string, number for given value. So for example if we have any string and we want to check if the string contains the specific pattern or not then we can use the regex expression, a real life example if you have seen search options for many site when we type first two or three letter of the string the search box start displaying us some suggestion, these suggestion are coming because of the regex format only. RegEx is a package which we need to import for uses.
Syntax
Given below is the syntax for the regex for the go language:
- First we need to import the package regexp.
- On the regex we can use the method Compile with passing any pattern which we want to match from any string or full sentence.
- Finally on the reg-expression variable we can call the required methods, the method can be for matching, replacing or for finding the position of the given pattern in the given string or sentence.
reg-expression := regexp.Compile(pattern)
reg-expression.method
How does RegEx work in Go Language?
- Before using we need to import the package regexp from go language.
- Once we imported the package, we can create any pattern with use of the method Compile over the imported regexp.
- We capture the compiled pattern into a variable.
- The captured compiled variable can be used for performing various operations on any given sentence or string, for example we can search for the pattern, we can replace the found pattern and we can find the number of times we found the pattern etc.
Examples of Golang RegEx
Given below are the examples mentioned:
To run these examples we can create a file with name regex.go and paste the example code on the file and run the command go run regex.go and we can get the output.
Example #1
In the below example we are checking if the pattern of the latter Ra is there in the given string or not. We used the method MustCompiled and inside this method we have passed the Ra pattern and capture the output in another variable and checking the in various strings for pattern Ra.
Code:
package main
import (
"fmt"
"regexp"
)
func main() {
regularExpression := regexp.MustCompile("Ra")
fmt.Println("Checking of the string pattern Ra ",regularExpression.FindString("Ranjan"))
fmt.Println("Checking of the string pattern Ra",regularExpression.FindString("Ajay"))
fmt.Println("Checking of the string pattern Ra",regularExpression.FindString("Rakesh"))
}
Output:
Example #2
In the below example we are checking the pattern availability and pattern start index to the end index inside the given string of names of the places.
Code:
package main
import (
"fmt"
"regexp"
)
func main() {
regexExpression := regexp.MustCompile("rat")
fmt.Println("Checking of the pattern position",regexExpression.FindStringIndex("Surat"))
fmt.Println("Checking of the pattern position",regexExpression.FindStringIndex("Chennai"))
fmt.Println("Checking of the pattern position",regexExpression.FindStringIndex("Gujrat"))
fmt.Println("Checking of the pattern position",regexExpression.FindStringIndex("Bharat"))
}
Output:
Example #3
In the below example we are checking the patter [a-z] which means any letter between a to z. We have taken string Ranking where for a we have taken a regex pattern like [a-z] and comparing the with some sentences.
Code:
package main
import (
"fmt"
"regexp"
)
func main() {
regexExpression := regexp.MustCompile("R([a-z]+)nking")
fmt.Println("The patter checking is",regexExpression.FindStringSubmatch("Ranking increasing"))
fmt.Println("The patter checking is",regexExpression.FindStringSubmatch("Ranking"))
fmt.Println("The patter checking is",regexExpression.FindStringSubmatch("Burden"))
}
Output:
Example #4
Below is an example where we are replacing letters with other letters of the given string names. Here we have used the ReplaceAll method of the regex which allows us to replace all the letters available inside the string.
Code:
package main
import (
"fmt"
"regexp"
)
func main() {
regexExpression := regexp.MustCompile("R")
newLetter := "M"
fmt.Println("The string after replacing with pattern",regexExpression.ReplaceAllString("Ranjan", newLetter))
fmt.Println("The string after replacing with pattern",regexExpression.ReplaceAllString("Raaja", newLetter))
fmt.Println("The string after replacing with pattern",regexExpression.ReplaceAllString("Raani", newLetter))
}
Output:
Example #5
In the below example we are fetching the numbers only from the string. We have used the method FindAllString, in this method we are passing two attributes one is for number depression and second is the number of match attributes we want to fetch.
Code:
package main
import (
"fmt"
"regexp"
)
func main() {
string := "Ra13, Pa67, S-60 AC-21, UP44"
expression := regexp.MustCompile(`[-]?\d[\d]*[\]?[\d{2}]*`)
res := expression.FindAllString(string, 4)
for _, attribute := range res {
fmt.Println("The number with string is:", attribute)
}
}
Output:
Example #6
In the below example we are fetching the pattern of string. So if pattern is 2 or 3 or any number of times repeated it will be fetched.
Code:
package main
import (
"fmt"
"regexp"
)
func main() {
regex := regexp.MustCompile(`learning`)
fmt.Println(regex.FindAllString("We should never stop learning", -1))
fmt.Println(regex.FindAllString("Never stop learning ,learning gives us meaning to live", 2))
fmt.Println(regex.FindAllString("We should learn go language", 0))
fmt.Println(regex.FindAllString("Win war by love not by power", 1))
}
Output:
Conclusion
In this article, we saw the basic flow of the regex in the go language, we saw uses with the help of some important examples. We saw how it worked and what is the main reason for use of the regex in go language.
Recommended Articles
This is a guide to Golang RegEx. Here we discuss the introduction, syntax, and working of RegEx in go language along with examples and code implementation. You may also have a look at the following articles to learn more –