Golang Regexp.ReplaceAllString
last modified April 20, 2025
This tutorial explains how to use the Regexp.ReplaceAllString
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.ReplaceAllString method replaces all matches of a regex pattern in a string with a specified replacement string.
Basic ReplaceAllString Example
The simplest use of ReplaceAllString
replaces all occurrences of
a pattern. Here we replace all digits with asterisks.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`\d`) original := "Order 12345 was placed on 2023-04-20" replaced := re.ReplaceAllString(original, "*") fmt.Println("Original:", original) fmt.Println("Replaced:", replaced) }
The pattern matches any digit character. ReplaceAllString
replaces
each digit with an asterisk. The method scans the entire input string.
Replacing with Capture Groups
We can use capture groups in the replacement string. This example reformats dates.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`(\d{4})-(\d{2})-(\d{2})`) original := "Event date: 2023-04-20" replaced := re.ReplaceAllString(original, "$2/$3/$1") fmt.Println("Original:", original) fmt.Println("Replaced:", replaced) }
The pattern captures year, month, and day groups. The replacement string rearranges them in MM/DD/YYYY format using $1, $2, $3 references.
Case-Insensitive Replacement
ReplaceAllString
can perform case-insensitive replacements when
using the (?i)
flag. This replaces all color spellings.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`(?i)colour`) original := "Colour, colour, COLOR, coloUr" replaced := re.ReplaceAllString(original, "color") fmt.Println("Original:", original) fmt.Println("Replaced:", replaced) }
The (?i)
flag makes the match case-insensitive. All variations of
"colour" are replaced with the American spelling "color".
Removing Text with Empty Replacement
Passing an empty string as replacement effectively removes matched patterns. This removes all HTML tags from a string.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`<[^>]+>`) original := "<h1>Title</h1><p>Paragraph</p>" replaced := re.ReplaceAllString(original, "") fmt.Println("Original:", original) fmt.Println("Replaced:", replaced) }
The pattern matches any text between angle brackets. The empty replacement string removes all HTML tags while preserving the content between them.
Replacing Multiple Patterns
For complex replacements, we can chain multiple ReplaceAllString
calls. This normalizes whitespace in a string.
package main import ( "fmt" "regexp" ) func main() { original := " Too many spaces here. " // Replace multiple spaces with single space re1 := regexp.MustCompile(`\s+`) step1 := re1.ReplaceAllString(original, " ") // Trim leading/trailing spaces re2 := regexp.MustCompile(`^\s|\s$`) final := re2.ReplaceAllString(step1, "") fmt.Println("Original:", original) fmt.Println("Final:", final) }
The first replacement condenses multiple whitespace characters. The second removes leading and trailing spaces. This produces clean, normalized text.
Using a Replacement Function
For dynamic replacements, we can use ReplaceAllStringFunc
. This
example converts temperatures from Fahrenheit to Celsius.
package main import ( "fmt" "regexp" "strconv" ) func main() { re := regexp.MustCompile(`(\d+)°F`) original := "Temperatures: 32°F, 68°F, 100°F" replaced := re.ReplaceAllStringFunc(original, func(match string) string { f, _ := strconv.Atoi(re.FindStringSubmatch(match)[1]) c := (f - 32) * 5 / 9 return fmt.Sprintf("%d°C", c) }) fmt.Println("Original:", original) fmt.Println("Replaced:", replaced) }
The pattern matches Fahrenheit temperatures. For each match, the function converts the value to Celsius. This shows dynamic replacement capabilities.
Escaping Special Characters
When replacing with literal strings containing special regex characters, we must escape them. This example safely replaces dollar amounts.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`\$\d+`) original := "Prices: $10, $20, $30" replacement := regexp.QuoteMeta("[REDACTED]") replaced := re.ReplaceAllString(original, replacement) fmt.Println("Original:", original) fmt.Println("Replaced:", replaced) }
regexp.QuoteMeta
escapes special characters in the replacement
string. This ensures the replacement is treated as literal text, not a pattern.
Source
Go regexp package documentation
This tutorial covered the Regexp.ReplaceAllString
method in Go
with practical examples of text replacement and manipulation.
Author
List all Go tutorials.