0Pricing
Go Academy · Lesson

Replacing Text

ReplaceAll patterns.

Replacing Text is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Pattern-Based Replacement

Regexps can rewrite text, not just find it. ReplaceAllString swaps every match with a replacement string.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("[0-9]+")
    fmt.Println(re.ReplaceAllString("a1b2c3", "#"))
}

Replacing Whole Words

A pattern like cat|dog lets you replace several alternatives in one pass.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("cat|dog")
    fmt.Println(re.ReplaceAllString("my cat and dog", "pet"))
}

Collapsing Whitespace

A common cleanup: replace runs of whitespace with a single space using \s+.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("\\s+")
    fmt.Println(re.ReplaceAllString("too   many    spaces", " "))
}

Referring to Groups

In the replacement, $1 inserts the first capture group, $2 the second. This rearranges matched parts.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("(\\d+)-(\\d+)")
    fmt.Println(re.ReplaceAllString("12-34", "$2-$1"))
}

Named Group References

With named groups you can use ${name} in the replacement for clarity.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("(?P<y>\\d{4})-(?P<m>\\d{2})")
    fmt.Println(re.ReplaceAllString("2026-05", "${m}/${y}"))
}

Safe Braces

Use ${1} braces when text follows a group reference, so $1 is not confused with a longer name.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("(\\d+)")
    fmt.Println(re.ReplaceAllString("7", "${1}px"))
}

Literal Replacement

ReplaceAllLiteralString ignores $ references and inserts the replacement exactly as written.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("[0-9]+")
    fmt.Println(re.ReplaceAllLiteralString("a1", "$money"))
}

Replace with a Function

ReplaceAllStringFunc calls your function for each match and substitutes whatever it returns. This enables computed replacements.

package main

import (
    "fmt"
    "regexp"
    "strings"
)

func main() {
    re := regexp.MustCompile("[a-z]+")
    out := re.ReplaceAllStringFunc("go is fun", strings.ToUpper)
    fmt.Println(out)
}

Computed Replacement

Inside the function you can transform each match however you like, here doubling each matched digit run by wrapping it.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("[0-9]+")
    out := re.ReplaceAllStringFunc("id5 and id9", func(m string) string {
        return "<" + m + ">"
    })
    fmt.Println(out)
}

Masking Sensitive Data

A practical use: redact numbers (like card-style digit runs) before logging.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("\\d{4}")
    fmt.Println(re.ReplaceAllString("card 1234 5678", "****"))
}

Working on Bytes

Byte-slice variants like ReplaceAll exist too, mirroring the string methods for binary or buffer data.

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("[0-9]+")
    out := re.ReplaceAll([]byte("a1b2"), []byte("#"))
    fmt.Println(string(out))
}

Quick Check

How do you insert the first capture group in a replacement string?

Recap: Replacing Text

You learned to rewrite text:

  • ReplaceAllString swaps matches; $1/${name} reference groups.
  • ReplaceAllLiteralString skips expansion.
  • ReplaceAllStringFunc computes each replacement.
package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile("(\\w+)\\s(\\w+)")
    fmt.Println(re.ReplaceAllString("Ada Lovelace", "$2, $1"))
}

Frequently asked questions

Is the “Replacing Text” lesson free?

Yes — the full text of “Replacing Text” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Replacing Text”?

ReplaceAll patterns. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Replacing Text” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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