This Go code demonstrates the usage of the regexp
package for regular expression matching and manipulation. Let's go through the code with inline comments and explanations:
// Importing necessary packages.
import (
"bytes"
"fmt"
"regexp"
)
// The main function, where the execution of the program begins.
func main() {
// Using MatchString to check if a string matches a regular expression pattern.
match, _ := regexp.MatchString("p([a-z]+)ch", "peach")
fmt.Println(match)
// Compiling a regular expression pattern for reuse.
r, _ := regexp.Compile("p([a-z]+)ch")
// Using MatchString with the compiled regex.
fmt.Println(r.MatchString("peach"))
// Finding the first match in the input string.
fmt.Println(r.FindString("peach punch"))
// Finding the start and end indices of the first match.
fmt.Println("idx:", r.FindStringIndex("peach punch"))
// Finding submatches of the first match.
fmt.Println(r.FindStringSubmatch("peach punch"))
// Finding start and end indices of submatches of the first match.
fmt.Println(r.FindStringSubmatchIndex("peach punch"))
// Finding all matches in the input string.
fmt.Println(r.FindAllString("peach punch pinch", -1))
// Finding all start and end indices of submatches in all matches.
fmt.Println("all:", r.FindAllStringSubmatchIndex(
"peach punch pinch", -1))
// Finding a specific number of matches.
fmt.Println(r.FindAllString("peach punch pinch", 2))
// Matching using a byte slice.
fmt.Println(r.Match([]byte("peach")))
// Compiling a regular expression using MustCompile for simplicity.
r = regexp.MustCompile("p([a-z]+)ch")
fmt.Println("regexp:", r)
// Replacing all matches with a specified string.
fmt.Println(r.ReplaceAllString("a peach", "<fruit>"))
// Replacing all matches using a function.
in := []byte("a peach")
out := r.ReplaceAllFunc(in, bytes.ToUpper)
fmt.Println(string(out))
}
true
true
peach
idx: [0 5]
[peach ea]
[0 5 1 3]
[peach punch pinch]
all: [[0 5 1 3] [6 11 7 9] [12 17 13 15]]
[peach punch]
true
regexp: p([a-z]+)ch
a <fruit>
a PEACH
Explanation:
-
MatchString
andCompile
:MatchString
checks if a string matches a regular expression pattern.Compile
is used to compile a regular expression pattern for later use.
-
Finding Matches:
FindString
finds the first match in the input string.FindStringIndex
finds the start and end indices of the first match.FindStringSubmatch
finds submatches of the first match.FindStringSubmatchIndex
finds start and end indices of submatches of the first match.FindAllString
finds all matches in the input string.FindAllStringSubmatchIndex
finds all start and end indices of submatches in all matches.FindAllString
can be used to find a specific number of matches.
-
Matching and Replacing:
Match
is used to match using a byte slice.MustCompile
is used to simplify regular expression compilation.ReplaceAllString
replaces all matches with a specified string.ReplaceAllFunc
replaces all matches using a function (bytes.ToUpper
in this case).
These examples demonstrate common operations with regular expressions using the regexp
package in Go. Regular expressions provide powerful and flexible pattern matching capabilities.