Go - Regular Expression



In Golang, regular expressions, or regex, is a common task in programming that provides a built-in package called "regexp" that allows developers to use regular expressions in their programs. In this tutorial, we will explore how to use regular expressions in Golang and the different functions and methods available in the "regexp" package.

What is Regular Expression?

A regular expression, or regex, is a sequence of characters that define a search pattern. Regular expressions are used to search, replace, and manipulate text. They are a powerful tool for text processing and data validation.

Using Regexp in Go

Golang provides a built-in "regexp" package that allows developers to use regular expressions in their programs. The "regexp" package contains functions and methods that can be used to match and manipulate text using regular expressions.

Matching Strings Using Regular Expressions

You can create a regular expression pattern and check if it matches a given string. This is done using functions like MatchString, which takes a pattern and a string as input and returns a boolean value indicating whether the pattern is found in the string.

Example

The following example shows how to create a regular expression pattern and match it with a string

In this example, we create a regular expression pattern that matches the word "Golang" and then use the MatchString function to match the pattern with the string "I love Golang programming language." The matched variable will contain a boolean value indicating whether the pattern was matched or not.

package main
import (
   "fmt"
   "regexp"
)

func main() {
   pattern := "Golang"
   text := "I love Golang programming language."
   matched, _ := regexp.MatchString(pattern, text)
   fmt.Println(matched)
}

Output

It will generate the following output −

true

Regular Expression Functions and Methods

The "regexp" package provides several functions and methods that can be used to match and manipulate text using regular expressions. Here are some of the most commonly used ones:

1. MatchString()

The MatchString function is used to match a regular expression pattern with a string. It returns a boolean value indicating whether the pattern was matched or not.

matched, err := regexp.MatchString(pattern, str)

2. FindString()

The FindString function is used to find the first occurrence of a regular expression pattern in a string. It returns the matched string.

re := regexp.MustCompile(pattern)
result := re.FindString(str)

3. FindAllString

The FindAllString function is used to find all occurrences of a regular expression pattern in a string. It returns a slice of matched strings.

re := regexp.MustCompile(pattern)
results := re.FindAllString(str, -1)

4. ReplaceAllString()

The ReplaceAllString function is used to replace all occurrences of a regular expression pattern in a string with a new string.

re := regexp.MustCompile(pattern)
newStr := re.ReplaceAllString(str, replacement)

Regular Expressions Example: Matching, Finding, & Replacing Strings

In this example, we first use the MatchString function to match a regular expression pattern with a string. We then use the FindString function to find the first occurrence of the pattern in the string, the FindAllString function to find all occurrences of the pattern, and the ReplaceAllString function to replace all occurrences of the pattern with a new string.

package main
import (
   "fmt"
   "regexp"
)

func main() {
   pattern := "go(lang)?"
   text := "I love Golang programming language. Go is awesome!"
   
   matched, _ := regexp.MatchString(pattern, text)
   fmt.Println(matched)
   
   match := regexp.MustCompile(pattern)
   fmt.Println(match.FindString(text))
   
   matches := match.FindAllString(text, -1)
   fmt.Println(matches)
   
   replaced := match.ReplaceAllString(text, "Golang")
   fmt.Println(replaced)
}

Output

It will generate the following output −

true
Golang
[Golang]
I love Golang programming language. Go is awesome!
Advertisements