Golang Regexp.ReplaceAll
last modified April 20, 2025
This tutorial explains how to use the Regexp.ReplaceAll
method in Go.
We'll cover regex replacement basics and provide practical examples.
A regular expression is a sequence of characters that defines a search pattern. It's used for pattern matching within strings.
The Regexp.ReplaceAll method replaces all occurrences of a regex pattern in a byte slice with a replacement string. It returns the modified slice.
Basic ReplaceAll Example
The simplest use of ReplaceAll
replaces all matches with a fixed
string. Here we replace all digits with asterisks.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`\d`) input := []byte("Order 12345 placed on 2025-04-20") result := re.ReplaceAll(input, []byte("*")) fmt.Println(string(result)) }
We compile a pattern matching any digit. ReplaceAll
replaces each
digit with an asterisk. The input and output are byte slices.
Using Capture Groups in Replacements
ReplaceAll
can reference capture groups from the pattern. This
example reformats dates from YYYY-MM-DD to MM/DD/YYYY.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`(\d{4})-(\d{2})-(\d{2})`) input := []byte("Event date: 2025-04-20") replacement := []byte("$2/$3/$1") result := re.ReplaceAll(input, replacement) fmt.Println(string(result)) }
The pattern captures year, month, and day groups. The replacement references these groups to rearrange the date format. $1 refers to the first group.
Conditional Replacements with Functions
ReplaceAllFunc
allows custom logic for each match. Here we
capitalize specific words in a sentence.
package main import ( "bytes" "fmt" "regexp" "strings" ) func main() { re := regexp.MustCompile(`\b(go|rust|python)\b`) input := []byte("I prefer go over rust, but python is also great.") result := re.ReplaceAllFunc(input, func(match []byte) []byte { return bytes.ToUpper(match) }) fmt.Println(string(result)) }
The function receives each match as a byte slice. We convert matching words to uppercase. This provides more control than simple string replacements.
Escaping Special Characters
This example demonstrates replacing special regex characters with their escaped versions. This is useful when processing user input.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`([.*+?^${}()|\[\]\\])`) input := []byte("Special chars: . * + ? { } ( ) [ ]") result := re.ReplaceAll(input, []byte(`\$1`)) fmt.Println(string(result)) }
The pattern matches regex metacharacters. Each match is replaced with itself preceded by a backslash. $1 refers to the matched character.
Removing Sensitive Information
ReplaceAll
can redact sensitive data. Here we mask credit card
numbers while preserving the last four digits.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`\b(?:\d[ -]*?){13,16}\b`) input := []byte("Card: 4111 1111 1111 1111") result := re.ReplaceAll(input, []byte("[REDACTED]")) fmt.Println(string(result)) }
The pattern matches various credit card number formats. All matches are replaced with "[REDACTED]". This helps protect sensitive information.
Template-style Replacements
This example shows how to implement simple template replacements using regex. We replace placeholders with actual values.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`\{\{(\w+)\}\}`) input := []byte("Hello {{name}}, your balance is {{amount}}.") replacements := map[string]string{ "name": "John", "amount": "$100", } result := re.ReplaceAllFunc(input, func(match []byte) []byte { // Fix the warning by directly using the map lookup return []byte(replacements[string(re.ReplaceAll(match, []byte("$1")))]) }) fmt.Println(string(result)) }
The pattern matches {{key}} placeholders. The replacement function looks up each key in a map. This creates a simple template system.
Performance Optimization
For repeated replacements, pre-compiling both pattern and replacement can improve performance. This example demonstrates the technique.
package main import ( "fmt" "regexp" "time" ) func main() { re := regexp.MustCompile(`\b\w{4}\b`) input := []byte("This text contains several four-letter words.") replacement := []byte("****") start := time.Now() for i := 0; i < 10000; i++ { re.ReplaceAll(input, replacement) } fmt.Println("Optimized:", time.Since(start)) start = time.Now() for i := 0; i < 10000; i++ { regexp.MustCompile(`\b\w{4}\b`).ReplaceAll(input, []byte("****")) } fmt.Println("Unoptimized:", time.Since(start)) }
The benchmark shows pre-compiling patterns and replacements is much faster. Avoid recompiling for each replacement operation when possible.
Source
Go regexp package documentation
This tutorial covered the Regexp.ReplaceAll
method in Go with
practical examples of text replacement and transformation.
Author
List all Go tutorials.