A regular expression is a sequence of characters which define a search pattern. Go language support regular expressions. A regular expression is used for parsing, filtering, validating, and extracting meaningful information from large text, like logs, the output generated from other programs, etc.
In Go regexp, you are allowed to check whether the given slice byte contains any match of the specified regular expression pattern with the help of Match() function. This function is defined under the regexp package, so for accessing this method you need to import the regexp package in your program.
Syntax:
func Match(p string, s []byte) (result bool, err error)
Here, p represents the pattern and s represents a slice of bytes. This function returns true if the pattern matched or return false if the pattern does not match. And also return an error if found.
Example 1:
// Go program to illustrate how to check // the given regexp present in the given slice package main import ( "fmt" "regexp") // Main function func main() { // Creating and initializing // slice of bytes // Using shorthand declaration s1 := []byte{'G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S'} s2 := []byte{'g', 'f', 'g'} // Pattern p1 := "G" p2 := "g" p3 := "^^" p4 := "@" // Matching pattern // Using Match() function res1, e := regexp.Match(p1, s1) fmt.Println("Result and Error is:", res1, e) res2, e := regexp.Match(p2, s1) fmt.Println("Result and Error is:", res2, e) res3, e := regexp.Match(p3, s1) fmt.Println("Result and Error is:", res3, e) res4, e := regexp.Match(p4, s1) fmt.Println("Result and Error is:", res4, e) res5, e := regexp.Match(p1, s2) fmt.Println("Result and Error is:", res5, e) res6, e := regexp.Match(p2, s2) fmt.Println("Result and Error is:", res6, e) res7, e := regexp.Match(p3, s2) fmt.Println("Result and Error is:", res7, e) res8, e := regexp.Match(p4, s2) fmt.Println("Result and Error is:", res8, e) } |
Output:
Result and Error is: true <nil> Result and Error is: false <nil> Result and Error is: true <nil> Result and Error is: false <nil> Result and Error is: false <nil> Result and Error is: true <nil> Result and Error is: true <nil> Result and Error is: false <nil>
Example 2:
// Go program to illustrate how to check // the given regexp present in the given slice package main import ( "fmt" "regexp") // Main function func main() { // Matching pattern in the // given slice of bytes // Using Match() function res1, e := regexp.Match(`eks`, []byte(`GeeksforGeeks`)) fmt.Println(res1, e) res2, e := regexp.Match(`BAN`, []byte(`Banana`)) fmt.Println(res2, e) res3, e := regexp.Match(`123`, []byte(`GeeksforGeeks`)) fmt.Println(res3, e) res4, e := regexp.Match(`e(ks`, []byte(`GeeksforGeeks`)) fmt.Println(res4, e) } |
Output:
true <nil> false <nil> false <nil> false error parsing regexp: missing closing ): `e(ks`
Recommended Posts:
- Golang | Extracting Regular Expression from the Slice
- Golang | Checking the string for the specified regular expression
- Golang | Finding Index of the Regular Expression present in Slice
- How to join the elements of the byte slice in Golang?
- How to find last index value of specified byte in slice of bytes in Golang?
- Finding the index value of specified byte in slice of bytes in Golang
- Golang | Splitting a Slice Separated by the Specified Expression
- Checking Slice of bytes for equality under Unicode case folding in Golang
- Golang | Extracting all the Regular Expression from the String
- Golang | Extracting a Regular Expression from the String
- Golang | Replacing all String which Matches with Regular Expression
- Golang | Finding Index of the Regular Expression present in String
- How to copy one slice into another slice in Golang?
- How to append a slice in Golang?
- How to sort a slice in Golang?
- How to Get First and Last Element of Slice in Golang?
- How to sort a slice of Search in Golang?
- How to sort a slice stable in Golang?
- Check if the given slice is sorted in Golang
- Golang | Splitting the slice after the specified separator
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

