We will explain how to work with regular expressions in Golang. A regular expression or regex is a sequence of characters to define a search pattern. It’a powerful technique used in most of programming language. Instead of writing many code lines, regex provides fast solution to handle everything in a single line of code to search, replace, extract, pattern matching etc.
In Golang, there are built-in package regexp
for regular
expressions. The package uses RE2 syntax standards that is also used by
other languages like Python, C, and Perl. The package contains
functions to deal with regular expressions. The major regex functions
are MatchString()
, FindString()
, FindStringIndex()
, FindStringSubmatch()
, FindStringSubmatchIndex()
, Compile()
, MustCompile()
and more.
So here in this tutorial, we will explain regex functions with examples to work with regular expressions.
1. MatchString Method
We can use the method regexp.MatchString()
from regex package for matching substring. Here in below example code, we want test if string starts with G
character. We will use caret(^) to match the beginning of a text in a
string. so we will use substring as “^G” to match in a string.
package main import ( "fmt" "regexp" ) func main() { str := "Golang regular expressions example" match, err := regexp.MatchString(`^G`, str) fmt.Println("Match: ", match, " Error: ", err) }
when we run the above example code, it will match the substring and return true. So it will display the output like below:
Match: true Error: <nil>
2. Compile or MustCompile method
We can use the Compile()
or MustCompile()
method to create the object of regex. The Compile()
method return error if there regular expression is invalid while MustCompile()
run without any error when there are invalid regular expression. So its always recommended to use Compile()
to create regex object. We can use these like below.
regexp1, err := regexp.Compile(`regexp`) regexp2 := regexp.MustCompile(`regexp`)
3. FindString method
We can use the FindString()
method to get the result of
the first match. If there are no match, the return value is an empty
string. Here in below example code, we will match the text ple
to exit at end of string. If string match then it will return the match
otherwise return empty string. In this example code, we have also used
method Compile()
to create a Regexp object. If we don’t want to get an error, we can use MustCompile()
method for the same.
package main import ( "fmt" "regexp" ) func main() { str := "Golang regular expressions example" regexp := regexp.Compile("ple$") fmt.Println(regexp.FindString(str)) }
when we run the above code, it display the following match string.
Match: ple Error: <nil>
4. FindStringIndex method
We can use the method FindStringIndex()
to get the the
starting and ending index of the leftmost match of the regular
expression. If there no match then it will return null value.
In below example code, we will find index of text p in a string.
package main import ( "fmt" "regexp" ) func main() { str := "Golang regular expressions example" regexp, err := regexp.Compile(`p`) match := regexp.FindStringIndex(str) fmt.Println("Match: ", match, " Error: ", err) }
when we will run above example code, it will return following output.
Match: [17 18] Error: <nil>
5. FindStringSubmatch method
We can use the method FindStringSubmatch()
to find the leftmost substring that matches the regex pattern. If there are no match then it will return a null value.
package main import ( "fmt" "regexp" ) func main() { str := "Golang regular expressions example" regexp, err := regexp.Compile(`l([a-z]+)g`) match := regexp.FindStringSubmatch(str) fmt.Println("Match: ", match, " Error: ", err) }
when we run above example code, it will return following result with leftmost substring from match.
Match: [lang an] Error: <nil>
6. FindStringSubmatchIndex Method
We can use the method FindStringSubmatchIndex()
to find index of leftmost substring that matches the regex pattern.
package main import ( "fmt" "regexp" ) func main() { str := "Golang regular expressions example" regexp, err := regexp.Compile(`l([a-z]+)g`) match := regexp.FindStringSubmatchIndex(str) fmt.Println("Match: ", match, " Error: ", err) }
when we run above example code, it will return following result with index from leftmost substring from match.
Match: [2 6 3 5] Error: <nil>
No comments:
Post a Comment