Take the 2-minute tour ×

I am not familiar with C-like syntaxes and would like to write code to find & replace, say, all 'A's to 'B's in a source string, say 'ABBA' with the Regexp package ReplaceAll or ReplaceAllString functions? How do I set up type Regexp, src and repl? Here's the ReplaceAll code snippet from the Go documentation:

// ReplaceAll returns a copy of src in which all matches for the Regexp
// have been replaced by repl.  No support is provided for expressions
// (e.g. \1 or $1) in the replacement text.
func (re *Regexp) ReplaceAll(src, repl []byte) []byte {
    lastMatchEnd := 0; // end position of the most recent match
    searchPos := 0;    // position where we next look for a match
    buf := new(bytes.Buffer);
    for searchPos     // Copy the unmatched characters before this match.

    // Now insert a copy of the replacement string, but not for a
    // match of the empty string immediately after another match.
    // (Otherwise, we get double replacement for patterns that
    // match both empty and nonempty strings.)
    if a[1] > lastMatchEnd || a[0] == 0 {
    lastMatchEnd = a[1];

    // Advance past this match; always advance at least one character.
    _, width := utf8.DecodeRune(src[searchPos:len(src)]);
    if searchPos+width > a[1] {
        searchPos += width
    } else if searchPos+1 > a[1] {
        // This clause is only needed at the end of the input
        // string.  In that case, DecodeRuneInString returns width=0.
    } else {
        searchPos = a[1]

// Copy the unmatched characters after the last match.

return buf.Bytes();


share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

This is a routine to do what you want:

package main
import ("fmt"; "regexp"; "os"; "strings";);
func main () {
    reg, error := regexp.Compile ("B");
    if error != nil {
        fmt.Printf ("Compile failed: %s", error.String ());
        os.Exit (1);
    output := string (reg.ReplaceAll (strings.Bytes ("ABBA"),
    			      strings.Bytes ("A")));
    fmt.Println (output);
share|improve this answer
how can I handle 2 replacements at once: A->B, B->A, so ABBA turns into BAAB in 1 step? –  user211499 Nov 16 '09 at 21:18
Why did you ask me instead of the guy whose answer you accepted? –  user181548 Nov 17 '09 at 4:48
shoot, I'm so rookie here, didn't even know that I can only accept 1 answer out of many and accepted both answers as I tried them out and they worked. –  user211499 Nov 17 '09 at 10:12
Oh, OK. Go's regexes are quite primitive and there is no simple way to do what you want (swap A and B), so you'd have to do three separate swaps (A->X, B->A, X->B) to make this work. –  user181548 Nov 17 '09 at 12:57
add comment

Here is a small example. You can also find good examples in he Regexp test class

package main 

import (

func main() {
  re,_ := regexp.Compile("e");
  input := "hello";
  replacement := "a";
  actual := string(re.ReplaceAll(strings.Bytes(input), strings.Bytes(replacement)));
  fmt.Printf("new patern %s",actual);
share|improve this answer
string () is what I needed. –  user181548 Nov 16 '09 at 1:13
add comment

Your Answer


