Matching using regexp in GoLang


Matching using regular expressions, or regex, is a common task in programming. Go, also known as Golang, provides a built-in package called "regexp" that allows developers to use regular expressions in their programs. In this article, we will explore how to use regular expressions in Go 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

Go 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.

Example

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

package main

import (
   "fmt"
   "regexp"
)

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

Output

true

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.

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 −

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.

FindString

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

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.

ReplaceAllString

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

Example

Here is an example of using these functions −

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

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

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.

Conclusion

Regular expressions are a powerful tool for text processing and data validation. Go provides a built-in "regexp" package that allows developers to use regular expressions in their programs. By using the functions and methods available in the "regexp" package, developers can match and manipulate text using regular expressions in a variety of ways. With regular expressions, developers can process text more efficiently and accurately in their Go programs.

Updated on: 25-Apr-2023

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements