0PricingLogin
Go Academy · Lesson

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

  1. regexp Basics
  2. Finding and Extracting
  3. Replacing Text
  4. Performance Tips
← Back to Go Academy