Home
Go
regexp.MatchString Use (Match)
This page was last reviewed on Sep 23, 2024.
Dot Net Perls
MatchString. In Go, we find an optimized regular expression engine. This runs in linear time, making complex patterns faster. It is located in the regexp package.
With MatchString, we see if a pattern can match a string. This tells us if the string satisfies the pattern's requirements. We can use Match to try to find a match within a byte slice.
regexp.Find
regexp.Split
regexp.ReplaceAllString
MatchString example. This program uses the MatchString method without first creating a Regexp instance. This means that a compiled expression is not reused.
Info We use some metacharacters in the pattern. The period means "any character" and the plus means "one or more."
Result The first return value (matched) is a boolean that is true if the pattern matched. The second is an error value (which may be nil).
package main import ( "fmt" "regexp" ) func main() { value := "cat" // See if this regular expression matches. matched, _ := regexp.MatchString(".+t", value) // Test the result. if matched { fmt.Println(true) } }
true
MustCompile example. A regexp can be compiled. Once compiled, it can be reused many times with no delays before calls. Many methods, like MatchString are available on regexp instances.
Info MustCompile() compiles a regular expression and returns the instance. It will cause an error (panic) if the pattern is invalid.
Here We loop over the strings in a slice and attempt to match each string with the compiled regular expression.
Slice
package main import ( "fmt" "regexp" ) func main() { // Compile this regular expression. var lettersDigits = regexp.MustCompile(`\w\w\w\d\d\d`) // Some strings to test. values := []string{"cat100", "dog200", "bird", "200fish"} // Loop over our strings and call MatchString on them. for i := range values { result := lettersDigits.MatchString(values[i]) fmt.Printf("%v = %v\n", values[i], result) } }
cat100 = true dog200 = true bird = false 200fish = false
Match. Unlike the MatchString method, Match() operates on a byte slice. We can convert a string literal to a byte slice to test the Match func.
package main import ( "fmt" "regexp" ) func main() { // With Match, we must match a byte slice, not a string. text := []byte("bird is Blue") re := regexp.MustCompile(`[bB]..e`) // Returns true because Blue matches the pattern. hasMatch := re.Match(text) fmt.Println(hasMatch) }
true
Find, FindAllString. With Find() and other methods like FindAllString(), a search is performed before matching occurs. So the entire string does not need to match—only parts do.
Summary. The regexp package is a key feature in Go. To achieve top performance, Go uses advanced logic to compile and execute these text programs.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 23, 2024 (new example).
Home
Changes
© 2007-2024 Sam Allen.