This article will explore how to work with Regular Expression in the Go programming language using the build Regexp package.
Golang Regexp Package
The Golang Regexp package provides you with methods and tools for searching, filtering, replacing, validating, and extracting strings using Regular expressions.
To use this package, we need to import it as:
To define regular expression patterns, we can use some predefined patterns as shown in the table below:
Pattern | Description |
. | Match any single character |
[abc] | Match a, b, or c |
\s | Match a whitespace character. \t\n\f\r |
[a-c] | Any character from a to c |
[^abc] | Negate and match all characters except a, b, and c |
\w | Match a word character |
a|b | Alternate operator. a or b, prefer a |
[a-z0-9] | Match any character from a to z or 0 to 9 |
\d | Match any digit between 0 to 9 |
^ | At the beginning of a text or line |
? | Match the prior character only once |
+ | Match the preceding character once or multiple times |
* | Match the preceding character 0 or multiple times |
$ | Match at the end of the text |
FindString() Method
The FindString() method returns the leftmost substring matching a specified pattern. If no match is found, the function returns an empty string.
An example code is as shown:
import (
"fmt"
"regexp"
)
funcmain() {
re := regexp.MustCompile("f([a-z]+)t")
fmt.Println(re.FindString("fruit"))
fmt.Println(re.FindString("fit"))
fmt.Println(re.FindString("false"))
fmt.Println(re.FindString("faith"))
fmt.Println(re.FindString("fix"))
}
In the previous example, we use the MustCompile method to compile the regular expression pattern to an optimized RegEx struct.
Then, we use the FindString() method to return the matching strings. The previous code should return:
fit
fait
Note: The function returns empty strings if there is no match.
FindStringIndex() Method
This method returns the starting and ending index of the leftmost substring matched by a specified regular expression.
Example:
import (
"fmt"
"regexp"
)
funcmain() {
re := regexp.MustCompile("f([a-z]+)t")
fmt.Println(re.FindStringIndex("fruit"))
}
The previous code should return the index of the matching substring. In our example, the code returns from index 0 to 5:
[0 5]
ReplaceAllString() Method
The ReplaceAllString() method is used to replace the text matching a specific pattern. It returns the copy and replaces all the matching strings.
We can illustrate this method using the following code:
import (
"fmt"
"regexp"
)
funcmain() {
re := regexp.MustCompile("f([a-z]+)t")
fmt.Printf("%q\n", re.ReplaceAllString("fruit", "food"))
}
The previous code should replace the matching string with the specified value. Since the whole string matches the specified pattern, the function replaces all the characters.
Split() Method
We can split a string using the Split() method and the specified pattern. An example code is as shown:
import (
"fmt"
"regexp"
)
funcmain() {
re := regexp.MustCompile("u")
fmt.Printf("%q\n", re.Split("fruit", -1))
fmt.Printf("%q\n", re.Split("thursday", -2))
fmt.Printf("%q\n", re.Split("function", 2))
}
The previous code defines a regular expression to match a single character. Then we use the Split method to split the provided string into various substrings where the pattern matches.
The resulting output is as shown:
["fr" "it"]
["th" "rsday"]
["f" "nction"]
Conclusion
This tutorial illustrates how to get started with Regular Expression in Go programming using the Regexp package. In addition, several methods were discussed to determine the right option for your needs. Check other Linux Hint articles and the documentation to learn more tips.