Golang | Extracting Regular Expression from the Slice
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 find a regular expression in the given string with the help of Find() method. This method returns a slice which is holding the text of the leftmost match in the original slice of the regular expression. Or return nil if no match found. This method is defined under the regexp package, so for accessing this method you need to import the regexp package in your program.
Syntax:
func (re *Regexp) Find(s []byte) []byte
Example 1:
// Go program to illustrate how to // find regexp from the given slice package main import ( "fmt" "regexp" ) // Main function func main() { // Finding regexp from the // given slice of bytes // Using Find() method m := regexp.MustCompile(`geeks?`) fmt.Printf( "%q\n" , m.Find([]byte(`GeeksgeeksGeeks, geeks`))) fmt.Printf( "%q\n" , m.Find([]byte(`Hello! geeksForGEEKs`))) fmt.Printf( "%q\n" , m.Find([]byte(`I like Go language`))) fmt.Printf( "%q\n" , m.Find([]byte(`Hello, Welcome`))) } |
Output:
"geeks" "geeks" "" ""
Example 2:
// Go program to illustrate how to // find regexp from the given slice package main import ( "fmt" "regexp" ) // Main function func main() { // Finding regexp from // the given slice // Using Find() method m := regexp.MustCompile(`language`) res := m.Find([]byte(`I like Go language this language is "+ "very easy to learn and understand`)) if res == nil { fmt.Printf( "Found: %q " , res) } else { fmt.Printf( "Found: %q" , res) } } |
Output:
Found: "language"
Recommended Posts:
- Golang | Extracting a Regular Expression from the String
- Golang | Extracting all the Regular Expression from the String
- Golang | Checking the byte of slice for specified regular expression
- Golang | Finding Index of the Regular Expression present in Slice
- Golang | Splitting a Slice Separated by the Specified Expression
- Golang | Checking the string for the specified regular expression
- 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 sort a slice in Golang?
- How to append a slice in Golang?
- How to split a slice of bytes in Golang?
- How to sort a slice of ints in Golang?
- How to repeat a slice of bytes in Golang?
- How to sort a slice stable in Golang?
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.