0Pricing
Go Academy · Lesson

regexp Basics

Compile and match patterns.

regexp Basics is a free Go Academy lesson on CoddyKit — lesson 1 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.

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

Compiling a Pattern

For repeated use, compile once with regexp.Compile. It returns a reusable *regexp.Regexp and an error for invalid patterns.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re, err := regexp.Compile("[0-9]+")
    fmt.Println(re.MatchString("abc123"), err)
}

MustCompile for Constants

regexp.MustCompile panics if the pattern is invalid. Use it for fixed patterns known at compile time, typically package-level variables.

package main

import (
    "fmt"
    "regexp"
)

var digits = regexp.MustCompile("[0-9]+")

func main() {
    fmt.Println(digits.MatchString("order 42"))
}

Character Classes

Square brackets define a set of allowed characters. [aeiou] matches any vowel; ranges like [a-z] match a span.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("[a-z]+")
    fmt.Println(re.MatchString("Hello"))
}

Predefined Classes

Shortcuts make common sets easy: \d is a digit, \w a word character, \s whitespace. In Go strings, escape the backslash.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("\\d+")
    fmt.Println(re.MatchString("abc 99"))
}

Quantifiers

Quantifiers say how many times:

  • * zero or more,
  • + one or more,
  • ? zero or one,
  • {2,4} a range.
package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("a{2,3}")
    fmt.Println(re.MatchString("aaa"))
    fmt.Println(re.MatchString("a"))
}

Anchors

Anchors pin a match: ^ means start of text and $ means end. Use both to require the whole string to match.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("^go$")
    fmt.Println(re.MatchString("go"))
    fmt.Println(re.MatchString("going"))
}

Alternation and Groups

The pipe | means OR, and parentheses group parts together. (cat|dog)s? matches cat, cats, dog, or dogs.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("(cat|dog)s?")
    fmt.Println(re.MatchString("dogs"))
}

Validating a Format

Anchored patterns make validators. Here we check a simple all-digit ZIP-style code of exactly five digits.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("^\\d{5}$")
    fmt.Println(re.MatchString("12345"))
    fmt.Println(re.MatchString("123"))
}

Match on Bytes

Methods come in string and byte-slice forms. Match works on a []byte, mirroring MatchString.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("[0-9]+")
    fmt.Println(re.Match([]byte("id7")))
}

Quick Check

Which function panics on an invalid pattern and is best for fixed patterns?

Recap: regexp Basics

You learned the fundamentals:

  • Match quickly with MatchString; compile with Compile or MustCompile.
  • Build patterns from classes, quantifiers, anchors, and groups.
  • Anchor with ^ and $ for full-string validation.
package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("^[a-z]+@[a-z]+$")
    fmt.Println(re.MatchString("hi@host"))
}

Frequently asked questions

Is the “regexp Basics” lesson free?

Yes — the full text of “regexp Basics” 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 “regexp Basics”?

Compile and match patterns. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “regexp Basics” 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