# Regex in go language with examples:Part-1

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682240689581/a33ca546-7a5c-4fea-925e-f369da8bb60c.png align="center")

### 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:

```go
package main

import (
    "fmt"
    "regexp"
)

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

Output:

```go
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:

```go
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:

```go
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:

```go
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:

```go
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:

```go
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:

```go
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:

```go
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:

```go
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](Link)

[https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA](Link)

[https://www.techwasti.com/](Link)

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

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

**Let’s connect on** [**Stackoverflow**](http://stackoverflow.com/users/3187349/maheshwar-ligade)**,** [**LinkedIn**](https://in.linkedin.com/in/maheshwar-ligade-14447841)**, &** [**Twitter**](https://twitter.com/MaheshwarLigade)**.**
