0Pricing
Go Academy · Lesson

Finding and Extracting

FindString and submatches.

Finding and Extracting is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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"))
}

FindAllString

FindAllString(s, n) returns every match as a slice. Pass -1 for all matches, or a positive limit.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("[0-9]+")
    fmt.Println(re.FindAllString("a1 b22 c333", -1))
}

Limiting Results

Pass a positive n to stop after that many matches.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("[0-9]+")
    fmt.Println(re.FindAllString("1 2 3 4", 2))
}

Finding the Index

FindStringIndex(s) returns the start and end byte positions of the first match as a two-element slice.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("cat")
    fmt.Println(re.FindStringIndex("a cat here"))
}

Capturing Groups

Parentheses create capture groups. FindStringSubmatch returns the whole match first, then each captured group.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("(\\d+)-(\\d+)")
    m := re.FindStringSubmatch("12-34")
    fmt.Println(m)
}

Using Submatch Values

Index into the submatch slice: element 0 is the full match, element 1 is the first group, and so on.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("(\\d+):(\\d+)")
    m := re.FindStringSubmatch("09:30")
    fmt.Println("hour", m[1], "min", m[2])
}

Named Groups

Name a group with (?P<name>...), then read it via SubexpNames alongside the submatch slice.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("(?P<area>\\d{3})-(?P<num>\\d{4})")
    m := re.FindStringSubmatch("555-1234")
    for i, name := range re.SubexpNames() {
        if name != "" {
            fmt.Println(name, m[i])
        }
    }
}

All Submatches

FindAllStringSubmatch(s, n) returns a slice of submatch slices, one per match. Great for parsing many records.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("(\\w+)=(\\d+)")
    all := re.FindAllStringSubmatch("x=1 y=2", -1)
    for _, m := range all {
        fmt.Println(m[1], m[2])
    }
}

No Match Behavior

When nothing matches, the Find methods return an empty string or a nil slice. Check before indexing to avoid a panic.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("[0-9]+")
    m := re.FindStringSubmatch("no digits")
    fmt.Println(m == nil)
}

Extracting Real Data

Combine groups to pull structured fields out of a line of text.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("(\\d{4})-(\\d{2})-(\\d{2})")
    m := re.FindStringSubmatch("date: 2026-05-30")
    fmt.Println("year", m[1])
}

Quick Check

Which element of a submatch slice is the whole match?

Recap: Finding and Extracting

You can now pull data out of text:

  • FindString and FindAllString return matches.
  • FindStringSubmatch returns the full match plus capture groups.
  • Use named groups and check for nil when nothing matches.
package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("(\\w+)@(\\w+)")
    m := re.FindStringSubmatch("user@host")
    fmt.Println(m[1], "at", m[2])
}

Frequently asked questions

Is the “Finding and Extracting” lesson free?

Yes — the full text of “Finding and Extracting” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Finding and Extracting”?

FindString and submatches. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Finding and Extracting” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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