Golang Regex Replace Example

Golang-Regex-Expression-Example

in this golang tutorial, I will explain how to work with regular expressions in Golang. The glonag have built-in package regexp for regular expressions.

What’s Regular Expression

A regular expression or regex is a sequence of characters to define a search pattern. You can do search, replace, extract, pattern matching etc using regular expression very easily. That help to remove extra lines of code by a single line of code.

The golang package uses RE2 syntax standards that is also used by other languages like Python, C, and Perl.There are following regex functions available –

  • MatchString()
  • FindString()
  • FindStringIndex()
  • FindStringSubmatch()
  • ReplaceAllString()
  • Compile()
  • MustCompile()
  • FindAllString

Let’s explain regex functions with examples to work with regular expressions –

MatchString – Golang regex match

The method regexp.MatchString() is used to match substring. Let’s check if string starts with 'Golang' character. We will use caret(^) to match the beginning of a text in a string. Regex pattern will be “^G” to match in a string.

package main
import (
"fmt"
"regexp"
)
func main() {
str := "Golang regular expressions example" 

match, err := regexp.MatchString(`^Golang`, str)     
fmt.Println("Match: ", match, " Error: ", err) 
}

Output:
The above code will return true if substring found into the source string :
Match: true Error:

Compile or MustCompile method

The Compile() or MustCompile() method to create the object of regex.The Compile() method return error if there regular expression is invalid while MustCompile() run without any error when there are invalid regular expression. The Compile() method recommended to use for regex object if you want good performance.

regexp1, err := regexp.Compile(regexp)
regexp2 := regexp.MustCompile(regexp)

FindString – Find String in Golang

The FindString() method to get the result of the first match. If there are no match, the return value is an empty string or match the empty string.

package main 
  
import ( 
    "fmt"
    "regexp"
) 
  
func main() {   
    
    str := "Golang expressions example" 
    
    regexp,_ := regexp.Compile("Gola([a-z]+)g")
    
    fmt.Println(regexp.FindString(str)) 
    
} 

Output:

Match: Golang Error:

FindStringIndex

The FindStringIndex() is used to get the the starting and ending index of the leftmost match of the regular expression. If there is no match found then it will return null value.

package main 
  
import ( 
    "fmt"
    "regexp"
) 
  
func main() {
str := "Golang regular expressions example" 

regexp, err := regexp.Compile(`exp`)

match := regexp.FindStringIndex(str)

fmt.Println("Match: ", match, " Error: ", err)
}

Output:
Match: [15 18] Error:

FindStringSubmatch

The FindStringSubmatch() to find the leftmost substring that matches the regex pattern. If there are no match found then it will return a null value.

package main 
  
import ( 
    "fmt"
    "regexp"
) 
  
func main() {   
    
    str := "Golang regular expressions example" 
    
    regexp, err := regexp.Compile(`p([a-z]+)e`)
        
        match := regexp.FindStringSubmatch(str)
        
        fmt.Println("Match: ", match, " Error: ", err) 
    
}

Output:
Match: [pre r] Error:

FindAllString

This method is used to get a slice of matched string. A return value of nil indicates no match.

package main 
  
import ( 
    "fmt"
    "regexp"
) 
  
func main() {   
    
    str := "Golang regular expressions example" 
    
    regexp, err := regexp.Compile(`p([a-z]+)e`)
        
        match := regexp.FindAllString(str, 2)
        
        fmt.Println("Match: ", match, " Error: ", err) 
    
}

Output:
Match: [pre ple] Error:

ReplaceAllString

The ReplaceAllString method is used to replace all string and returns copy of the source string.

package main 
  
import ( 
    "fmt"
    "regexp"
) 
  
func main() {   
    
    str := "Golang regular expressions example" 
    
    regexp, err := regexp.Compile(`examp([a-z]+)e`)
        
        match := regexp.ReplaceAllString(str, "tutorial")
        
        fmt.Println("Match: ", match, " Error: ", err) 
    
}

Output:

Match: Golang regular expressions tutorial Error:

Leave a Reply