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

How to Use regex to replace in Go

How to use regex to replace in Go

=====================================================

Regular expressions (regex) are a powerful tool for text manipulation, and Go provides a robust regexp package to work with them. In this guide, we'll explore how to use regex to replace text in Go, covering the basics, common edge cases, and performance tips.

Quick Example


Here's a minimal example that replaces all occurrences of "old" with "new" in a string:

package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile("old")
	newStr := re.ReplaceAllString("This is an old string", "new")
	fmt.Println(newStr) // Output: "This is a new string"
}

To use this code, make sure to install the regexp package by running go get -u golang.org/x/text/regexp.

Step-by-Step Breakdown


Let's walk through the code line by line:

  • re := regexp.MustCompile("old"): This line compiles the regex pattern "old" into a Regexp object. The Compile function returns a Regexp object if the pattern is valid, or an error if it's not.
  • newStr := re.ReplaceAllString("This is an old string", "new"): This line uses the ReplaceAllString method to replace all occurrences of "old" with "new" in the input string.
  • fmt.Println(newStr): This line prints the resulting string to the console.

Handling Edge Cases


Here are some common edge cases to consider:

Empty/null input

func main() {
	re := regexp.MustCompile("old")
	newStr := re.ReplaceAllString("", "new")
	fmt.Println(newStr) // Output: ""
}

In this case, the input string is empty, so the output is also empty.

Invalid input

func main() {
	re := regexp.MustCompile("[")
	_, err := re.ReplaceAllString("This is an old string", "new")
	fmt.Println(err) // Output: error: invalid regex pattern
}

In this case, the regex pattern is invalid, so the Compile function returns an error.

Large input

func main() {
	re := regexp.MustCompile("old")
	longStr := strings.Repeat("This is an old string", 1000)
	newStr := re.ReplaceAllString(longStr, "new")
	fmt.Println(newStr) // Output: replaced string
}

In this case, the input string is very large, but the ReplaceAllString method can still handle it efficiently.

Unicode/special characters

func main() {
	re := regexp.MustCompile(utf8.RuneToString([]rune{0x00e9})) // é
	newStr := re.ReplaceAllString("Café", "e")
	fmt.Println(newStr) // Output: "Cafe"
}

In this case, the input string contains a Unicode character (é), which is correctly matched and replaced by the regex pattern.

Common Mistakes


Here are some common mistakes developers make when using regex to replace in Go:

Mistake 1: Not compiling the regex pattern

func main() {
	re := regexp.MustCompile("old")
	newStr := regexp.ReplaceAllString("This is an old string", "new", "old") // WRONG!
	fmt.Println(newStr)
}

Corrected code:

func main() {
	re := regexp.MustCompile("old")
	newStr := re.ReplaceAllString("This is an old string", "new")
	fmt.Println(newStr)
}

Mistake 2: Not checking for errors

func main() {
	re := regexp.MustCompile("[")
	_, _ = re.ReplaceAllString("This is an old string", "new") // WRONG!
	fmt.Println("Replaced string")
}

Corrected code:

func main() {
	re := regexp.MustCompile("[")
	newStr, err := re.ReplaceAllString("This is an old string", "new")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(newStr)
}

Mistake 3: Using the wrong regex flags

func main() {
	re := regexp.MustCompile("old", regexp.IgnoreCase) // WRONG!
	newStr := re.ReplaceAllString("This is an OLD string", "new")
	fmt.Println(newStr)
}

Corrected code:

func main() {
	re := regexp.MustCompile("(?i)old") // case-insensitive
	newStr := re.ReplaceAllString("This is an OLD string", "new")
	fmt.Println(newStr)
}

Performance Tips


Here are some performance tips for using regex to replace in Go:

Tip 1: Compile the regex pattern only once

func main() {
	re := regexp.MustCompile("old")
	for i := 0; i < 1000; i++ {
		newStr := re.ReplaceAllString("This is an old string", "new")
		fmt.Println(newStr)
	}
}

Tip 2: Use the ReplaceAllString method instead of ReplaceAll

func main() {
	re := regexp.MustCompile("old")
	newStr := re.ReplaceAllString("This is an old string", "new") // faster
	// newStr := re.ReplaceAll([]byte("This is an old string"), []byte("new")) // slower
	fmt.Println(newStr)
}

Tip 3: Use the MustCompile function for simple patterns

func main() {
	re := regexp.MustCompile("old") // slower
	// re := regexp.MustCompile("old") // faster
	newStr := re.ReplaceAllString("This is an old string", "new")
	fmt.Println(newStr)
}

FAQ


Q: What is the difference between Compile and MustCompile?

A: Compile returns a Regexp object if the pattern is valid, or an error if it's not. MustCompile panics if the pattern is invalid.

Q: How do I match Unicode characters in my regex pattern?

A: Use the utf8.RuneToString function to convert the Unicode character to a string.

Q: Can I use the ReplaceAll method with a []byte input?

A: Yes, but it's slower than using the ReplaceAllString method.

Q: How do I make my regex pattern case-insensitive?

A: Use the (?i) flag at the beginning of your pattern.

Q: Can I use regex to replace text in a file?

A: Yes, but you'll need to read the file into 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