0PricingLogin
Go Academy · Lesson

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

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