Golang: regexp

By Xah Lee. Date: .

Here's example of using regular expression to replace text.

package main

import "fmt"

import "regexp"

var longtext = `Alice was beginning to get very tired of sitting by her
sister on the bank, and of having nothing to do: once or twice she had
peeped into the book her sister was reading, but it had no pictures or
conversations in it, «and what is the use of a book,» thought Alice «without
pictures or conversation?».`

func main() {
    var re = regexp.MustCompile(`(?i:alice)`)
    var replaceStr = "Mary"
    var result = re.ReplaceAllLiteralString(longtext, replaceStr)
    fmt.Printf("%v\n", result)

}

Reference

https://golang.org/pkg/regexp/

If you have a question, put $5 at patreon and message me.

Golang

  1. Compile, Run
  2. Package, Import
  3. Comment
  4. Print
  5. String
  6. Rune
  7. Variable
  8. Zero Value
  9. Constant
  10. If Then Else
  11. Switch/Case
  12. Loop
  13. Basic Types
  14. Array
  15. Slice
  16. Map
  17. Struct
  18. Function
  19. regexp
  20. Read File
  21. Write to File
  22. Walk Dir
  23. Check File Exist
  24. System Call
  25. Pointer
  26. Defer
  27. Random Number

Examples

  1. Validate Links
  2. Generate Sitemap

Reference

  1. Go Spec