Regular Expressions in Go
A regular expression (Regular Expression, abbreviated as regex or regexp) is a powerful tool for matching strings.
By defining a pattern, regular expressions can quickly search for, replace, or extract strings that match that pattern; for details, seeRegular Expression tutorial。
In Go, regular expressions are implemented through the regexp package.
In Go, the regexp package
The Go standard library provides the regexp package for handling regular expressions. The following are regexp commonly used functions and methods in the package:
-
CompileandMustCompile
used to compile a regular expression.
Compile Returns a *Regexp object and an error, while MustCompile panics directly when compilation fails.
2.
MatchString
checks whether a string matches a regular expression.
FindString and FindAllString
used to find matching strings.FindString returns the first match,FindAllString returns all matches.
ReplaceAllString
used to replace matching strings.
Split
splits a string according to a regular expression.
Basic regular expression syntax
The following are some commonly used regular expression syntax:
.: Matches any single character (except a newline).*: matches the preceding character zero or more times.+: matches the preceding character one or more times.?: matches the preceding character zero or one time.\d: matches a digit character (equivalent to[0-9])。\w: matches a letter, digit, or underscore (equivalent to[a-zA-Z0-9_])。\s: matches a whitespace character (including space, tab, newline, etc.).[]: matches any one character inside the brackets (for example[abc]Matchesa、borc)。^: matches the start of a string.$: matches the end of a string.
Example Code
The following are some examples of using regular expressions in Go:
Example 1: Check whether a string matches a regular expression
Example
package
main
import
(
"fmt"
"regexp"
)
func
main
()
{
pattern
:=
^[a-zA-Z0-9]+$
regex
:=
regexp
.
MustCompile
(
pattern
)
str
:=
"Hello123"
if
regex
.
MatchString
(
str
)
{
fmt
.
Println
(
"string matches the regular expression"
)
}
else
{
fmt
.
Println
(
"string does not match the regular expression"
)
}
}
Example 2: Find matching strings
Example
package
main
import
(
"fmt"
"regexp"
)
func
main
()
{
pattern
:=
\d+
regex
:=
regexp
.
MustCompile
(
pattern
)
str
:=
"I have 3 apples and 5 bananas"
matches
:=
regex
.
FindAllString
(
str
,
1
)
fmt
.
Println
(
"numbers found:"
,
matches
)
}
Example 3: Replace matching strings
Example
package
main
import
(
"fmt"
"regexp"
)
func
main
()
{
pattern
:=
\s+
regex
:=
regexp
.
MustCompile
(
pattern
)
str
:=
"Hello World"
result
:=
regex
.
ReplaceAllString
(
str
,
" "
)
fmt
.
Println
(
"replaced string:"
,
result
)
}
Example 4: Split a string
Example
package
main
import
(
"fmt"
"regexp"
)
func
main
()
{
pattern
:=
,
regex
:=
regexp
.
MustCompile
(
pattern
)
str
:=
"apple,banana,orange"
parts
:=
regex
.
Split
(
str
,
1
)
fmt
.
Println
(
"split string:"
,
parts
)
}
Notes
-
Performance Issues
Regular expression matching and replacement operations can consume considerable resources, especially when processing large amounts of data. It is recommended to use them with caution in performance-sensitive scenarios. 2.
Escape character
In Go, the backslash in a regular expression \ needs to be written as \\, because the backslash is also an escape character in strings.
Error handling
Use Compile function, be sure to check the returned error to avoid program crashes.