Try it yourself with our free Regex Tester tool — runs entirely in your browser, no signup needed.

How to Use regex to match in Go

How to Use Regex to Match in Go

Regular expressions (regex) are a powerful tool for pattern matching in strings. Go provides a built-in regexp package that allows you to use regex to match, validate, and extract data from strings. In this guide, we will explore how to use regex to match in Go, covering the basics, common edge cases, and performance tips.

Quick Example

Here is a minimal example that demonstrates how to use regex to match a pattern in a string:

package main

import (
	"fmt"
	"regexp"
)

func main() {
	// Compile the regex pattern
	pattern := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)

	// Test the pattern against a string
	input := "My birthday is 1990-02-12"
	if pattern.MatchString(input) {
		fmt.Println("Match found!")
	} else {
		fmt.Println("No match found.")
	}
}

This code compiles a regex pattern that matches a date in the format YYYY-MM-DD and tests it against a string.

Step-by-Step Breakdown

Let's break down the code line by line:

  1. import "regexp": We import the regexp package, which provides the regex functionality.
  2. pattern := regexp.MustCompile(\d{4}-\d{2}-\d{2}): We compile the regex pattern using the Compile function. The pattern \d{4}-\d{2}-\d{2} matches a date in the format YYYY-MM-DD. The \d matches a digit, and the {4}, {2}, and {2} specify the exact number of digits to match.
  3. input := "My birthday is 1990-02-12": We define a string to test the pattern against.
  4. if pattern.MatchString(input) { ... }: We use the MatchString function to test the pattern against the input string. If the pattern matches, the function returns true, and we print a success message.

Handling Edge Cases

Here are some common edge cases to consider:

Empty/Null Input

func main() {
	pattern := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
	input := ""
	if pattern.MatchString(input) {
		fmt.Println("Match found!")
	} else {
		fmt.Println("No match found.")
	}
}

In this case, the MatchString function returns false, as the input string is empty.

Invalid Input

func main() {
	pattern := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
	input := "Invalid date format"
	if pattern.MatchString(input) {
		fmt.Println("Match found!")
	} else {
		fmt.Println("No match found.")
	}
}

In this case, the MatchString function returns false, as the input string does not match the pattern.

Large Input

func main() {
	pattern := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
	input := "Lorem ipsum dolor sit amet, consectetur adipiscing elit. My birthday is 1990-02-12."
	if pattern.MatchString(input) {
		fmt.Println("Match found!")
	} else {
		fmt.Println("No match found.")
	}
}

In this case, the MatchString function returns true, as the pattern is found within the larger input string.

Unicode/Special Characters

func main() {
	pattern := regexp.MustCompile(`[\p{L}]+`)
	input := "Hello, monde!"
	if pattern.MatchString(input) {
		fmt.Println("Match found!")
	} else {
		fmt.Println("No match found.")
	}
}

In this case, the MatchString function returns true, as the pattern matches the Unicode characters in the input string. The \p{L} matches any Unicode letter.

Common Mistakes

Here are some common mistakes to avoid:

Mistake 1: Not compiling the pattern

func main() {
	pattern := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
	input := "My birthday is 1990-02-12"
	if pattern.Match(input) { // incorrect: should be MatchString
		fmt.Println("Match found!")
	} else {
		fmt.Println("No match found.")
	}
}

Corrected code:

func main() {
	pattern := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
	input := "My birthday is 1990-02-12"
	if pattern.MatchString(input) {
		fmt.Println("Match found!")
	} else {
		fmt.Println("No match found.")
	}
}

Mistake 2: Not handling errors

func main() {
	pattern := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
	input := "My birthday is 1990-02-12"
	pattern.MatchString(input) // ignore error
}

Corrected code:

func main() {
	pattern := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
	input := "My birthday is 1990-02-12"
	if err := pattern.MatchString(input); err != nil {
		fmt.Println("Error:", err)
	} else {
		fmt.Println("Match found!")
	}
}

Mistake 3: Using the wrong package

import "strings"

Corrected code:

import "regexp"

Performance Tips

Here are some performance tips to keep in mind:

  1. Compile patterns only once: Compiling a pattern can be expensive. If you need to use the same pattern multiple times, compile it only once and reuse the compiled pattern.
  2. Use MustCompile instead of Compile: If you're certain that your pattern is valid, use MustCompile instead of Compile. MustCompile panics if the pattern is invalid, whereas Compile returns an error.
  3. Use MatchString instead of Match: If you're only matching against a string, use MatchString instead of Match. MatchString is faster and more efficient.

FAQ

Q: What is the difference between Compile and MustCompile?

A: Compile returns an error if the pattern is invalid, whereas MustCompile panics if the pattern is invalid.

Q: How do I match a pattern against a large input string?

A: Use MatchString instead of Match. MatchString is faster and more efficient for large input strings.

Q: Can I use regex to match Unicode characters?

A: Yes, Go's regex engine supports Unicode characters. Use the \p{L} pattern to match any Unicode letter.

Q: How do I handle errors when using regex?

A: Use the err variable to check for errors when calling MatchString or Compile.

Q: Can I use regex to match against a byte slice?

A: No, Go's regex engine only supports matching against strings. If you need to match against a byte slice, convert it to a string first.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp