Regex in go language with examples:Part-1
Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...
Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...

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.
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
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
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
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
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
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://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.