Finding and Extracting
FindString and submatches.
Beyond Match
Matching tells you if a pattern exists. The Find family tells you what matched and where, so you can extract data.
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile("[0-9]+")
fmt.Println(re.FindString("order 42 ready"))
}FindString
FindString(s) returns the first match as a string, or an empty string if there is none.
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile("[a-z]+")
fmt.Printf("%q\n", re.FindString("123abc456"))
}All lessons in this course
- regexp Basics
- Finding and Extracting
- Replacing Text
- Performance Tips