In Go, you can use the regexp
package to check if a string matches a regular expression pattern. Here's an example code snippet:
package main
import (
"fmt"
"regexp"
)
func main() {
// Define the regular expression pattern
pattern := "[a-zA-Z]+"
// Compile the regular expression
regex, err := regexp.Compile(pattern)
if err != nil {
fmt.Println("Invalid regular expression pattern")
return
}
// Input string to be matched
input := "Hello"
// Check if the input string matches the pattern
if regex.MatchString(input) {
fmt.Println("Input string matches the pattern")
} else {
fmt.Println("Input string does not match the pattern")
}
}
In this example, the pattern [a-zA-Z]+
matches any sequence of one or more alphabetical characters. Replace the pattern
variable with your desired regular expression pattern. The MatchString
method of the compiled regular expression object is used to check if the input string matches the pattern.