regexp Basics
Compile and match patterns.
What Is a Regular Expression?
A regular expression (regexp) is a pattern that describes a set of strings. Go provides them through the regexp package.
They are perfect for validating and searching text.
package main
import (
"fmt"
"regexp"
)
func main() {
matched, _ := regexp.MatchString("go+", "gooo")
fmt.Println(matched)
}Quick Match with MatchString
regexp.MatchString(pattern, s) reports whether the pattern appears anywhere in s. It returns a bool and an error.
package main
import (
"fmt"
"regexp"
)
func main() {
ok, _ := regexp.MatchString("cat", "the cat sat")
fmt.Println(ok)
}All lessons in this course
- regexp Basics
- Finding and Extracting
- Replacing Text
- Performance Tips