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:
package main
import (
"fmt"
"regexp"
)
func main() {
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:
package main
import (
"fmt"
"regexp"
)
func main() {
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"
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
05 Sep, 2019
Like Article
Save Article