Follow

Follow

Regex in go language with examples:Part-1

Maheshwar Ligade's photo
Maheshwar Ligade
·May 7, 2023·

3 min read

Play this article

Table of contents

  • Introduction:
  • Conclusion:

Introduction:

Regular expressions, or regex, are a powerful tool for searching and manipulating text in Go and other programming languages. In Go, the regexp the package provides a simple and efficient way to work with regular expressions.

In this article, we will cover the basics of using regular expressions in Go, including creating patterns, searching for matches, and replacing text.

  1. Creating a regular expression pattern:

    To use regular expressions in Go, we need to create a pattern that defines what we are searching for. This pattern is created using a combination of normal characters and special characters that have special meanings in regular expressions.

Example:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    pattern := "hello"
    re := regexp.MustCompile(pattern)
    fmt.Println(re)
}

Output:

hello
  1. Searching for a match:

    Once we have a regular expression pattern, we can use it to search for matches in a string. The MatchString function in the regexp package can be used to search for a pattern in a string.

Example:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    pattern := "hello"
    re := regexp.MustCompile(pattern)

    str := "hello world"
    if re.MatchString(str) {
        fmt.Printf("%s matches %s\n", str, pattern)
    } else {
        fmt.Printf("%s does not match %s\n", str, pattern)
    }
}

Output:

hello world matches hello
  1. Extracting matched text:

    In addition to searching for a pattern in a string, we can also extract the text that matches the pattern using the FindString function in the regexp package. This function returns the first match found in the string.

Example:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    pattern := "hello"
    re := regexp.MustCompile(pattern)

    str := "hello world"
    match := re.FindString(str)

    fmt.Printf("Matched text: %s\n", match)
}

Output:

Matched text: hello
  1. Replacing matched text:

    In addition to searching for and extracting text, we can also replace text that matches a pattern with new text using the ReplaceAllString function in the regexp package.

Example:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    pattern := "hello"
    re := regexp.MustCompile(pattern)

    str := "hello world"
    replacement := "hi"
    replaced := re.ReplaceAllString(str, replacement)

    fmt.Printf("Replaced text: %s\n", replaced)
}

Output:

Replaced text: hi world
  1. Regular expression flags:

    In addition to the basic functionality of regular expressions, Go also provides flags that can modify the behavior of regular expressions. For example, the i flag can be used to make a regular expression case-insensitive.

Example:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    pattern := "(?i)hello"
    re := regexp.MustCompile(pattern)

    str := "Hello world"
    if re.MatchString(str) {
        fmt.Printf("%s matches %s\n", str, pattern)
    } else {
        fmt.Printf("%s does not match %s\n", str, pattern)
    }
}

Output:

Hello world matches (?i)hello

Conclusion:

Regular expressions are a powerful tool for searching and manipulating text in Go. We saw how to create a regular expression, match a regular expression against a string, and find sub matches in a regular expression.

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this