Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

What is Regex in Golang?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

A Regular Expression (or RegEx) is a special sequence of characters that defines a search pattern that is used for matching specific text. In Golang, there’s a built-in package for regular expressions, called the regexp package which contains all list of actions like filtering, replacing, validating, or extracting. It uses the RE2 syntax standards. The MatchString() function reports whether the string passed as a parameter contains any match of the regular expression pattern.


func MatchString(pattern string, s string)

Returns: matched bool, err error


// Golang program to illustrate the
// string matching using regexp
// in-built function
package main
import (
func main() {
    // string in which the pattern
    // is to be found
    str := "geeksforgeeks"
    // returns true if the pattern is present
    // in the string, else returns false
    // err is nil if the regexp is valid
    match1, err := regexp.MatchString("geeks", str)
    fmt.Println("Match: ", match1, " Error: ", err)
    // this returns false as the match
    // is unsuccessful
    str2 := "ComputerScience"
    match2, err := regexp.MatchString("geeks", str2)
    fmt.Println("Match: ", match2, "Error: ", err)
    // this will throw an error
    // as the pattern is not valid
    match3, err := regexp.MatchString("geek(s", str2)
    fmt.Println("Match: ", match3, "Error: ", err)


Match:  true  Error:  <nil>
Match:  false Error:  <nil>
Match:  false Error:  error parsing regexp: missing closing ): `geek(s`

To store complicated regular expressions for reuse later, Compile() method parses a regular expression and returns a Regexp object if successful which can be used to match the text. Prototype of the function is:

func Compile(expr string) (*Regexp, error)

There are other various methods provided in the regexp package to match strings as shown:

// Golang program to illustrate the
// string matching using regexp
// in-built functions
package main
import (
func main() {
    // a regex object which
    // can be reused later
    re, _ := regexp.Compile("geek")
    // string to be matched
    str := "I love geeksforgeeks"
    // returns the slice of first
    // and last index
    match := re.FindStringIndex(str)
    str2 := "I love computer science"
    // prints an empty slice
    // as there is no match
    match2 := re.FindStringIndex(str2)
    // finds the first or leftmost
    // match to a given pattern.
    re2, _ := regexp.Compile("[0-9]+-v.*g")
    // matches one or more numbers followed
    // by v and any number of characters upto g
    match3 := re2.FindString("20024-vani_gupta")
    // returns a slice of all successive
    // matches of the expression
    match4 := re.FindAllStringSubmatchIndex("I'am a geek at"+
                                        " geeksforgeeks", -1)
    // returns a copy and replaces
    // matches with the replacement string
    re3, _ := regexp.Compile(" ")
    match5 := re3.ReplaceAllString("All I do"+
                    " is code everytime.", "+")
    // returns a copy in which all matches are
    // replaced by return value of function
    re4, _ := regexp.Compile("[aeiou]+")
    match6 := re4.ReplaceAllStringFunc("All I do"+
           " is code everytime.", strings.ToUpper)


[7 11]
[[7 11] [15 19] [23 27]]
All I dO Is cOdE EvErytImE.

My Personal Notes arrow_drop_up
Last Updated : 19 May, 2020
Like Article
Save Article
Similar Reads