
- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
Go - Regular Expression
In Golang, regular expressions, or regex, is a common task in programming that provides a built-in package called "regexp" that allows developers to use regular expressions in their programs. In this tutorial, we will explore how to use regular expressions in Golang 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
Golang 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.
Matching Strings Using Regular Expressions
You can create a regular expression pattern and check if it matches a given string. This is done using functions like MatchString, which takes a pattern and a string as input and returns a boolean value indicating whether the pattern is found in the string.
Example
The following example shows how to create a regular expression pattern and match it with a string
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.
package main import ( "fmt" "regexp" ) func main() { pattern := "Golang" text := "I love Golang programming language." matched, _ := regexp.MatchString(pattern, text) fmt.Println(matched) }
Output
It will generate the following output −
true
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:
1. 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.
matched, err := regexp.MatchString(pattern, str)
2. FindString()
The FindString
function is used to find the first occurrence of a regular expression pattern in a string. It returns the matched string.
re := regexp.MustCompile(pattern) result := re.FindString(str)
3. 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.
re := regexp.MustCompile(pattern) results := re.FindAllString(str, -1)
4. ReplaceAllString()
The ReplaceAllString
function is used to replace all occurrences of a regular expression pattern in a string with a new string.
re := regexp.MustCompile(pattern) newStr := re.ReplaceAllString(str, replacement)
Regular Expressions Example: Matching, Finding, & Replacing Strings
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.
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
It will generate the following output −
true Golang [Golang] I love Golang programming language. Go is awesome!