Golang Regexp.Find
last modified April 20, 2025
This tutorial explains how to use the Regexp.Find
method in Go.
We'll cover basic pattern matching and provide practical examples.
A regular expression is a sequence of characters that defines a search pattern. It's used for pattern matching within strings.
The Regexp.Find method returns a slice holding the text of the leftmost match in the byte slice. It returns nil if no match is found.
Basic Regexp.Find Example
The simplest use of Regexp.Find
finds the first match in a byte
slice. Here we search for a simple word pattern.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`foo`) text := []byte("some foo bar foo baz") match := re.Find(text) fmt.Printf("Found: %q\n", match) // "foo" }
The method returns the first occurrence of "foo" in the byte slice. The result is a byte slice containing the matched text.
Finding Digits in a String
This example demonstrates finding the first sequence of digits in a string. We convert the string to a byte slice for the search.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`\d+`) text := []byte("Order 12345 was placed on 2025-04-20") match := re.Find(text) if match != nil { fmt.Printf("First number found: %s\n", match) // "12345" } else { fmt.Println("No numbers found") } }
The pattern \d+
matches one or more digits. The method returns the
first numeric sequence found in the input.
Case-Insensitive Matching
This example shows how to perform case-insensitive matching with Find
.
We use the (?i)
flag modifier in the pattern.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`(?i)hello`) text := []byte("HeLLo World, hElLo Go") match := re.Find(text) fmt.Printf("Found: %q\n", match) // "HeLLo" }
The (?i)
makes the pattern case-insensitive. The method returns
the first match regardless of case.
Finding Word Boundaries
This example demonstrates finding whole words using word boundaries.
The \b
metacharacter matches word boundaries.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`\bGo\b`) text := []byte("Golang Go programming in Go") match := re.Find(text) fmt.Printf("Found: %q\n", match) // "Go" }
The pattern matches the word "Go" only when it appears as a whole word. It won't match "Go" in "Golang".
Finding HTML Tags
This example shows how to find HTML tags in a document. We use a simple pattern to match tag names.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`<[a-zA-Z]+>`) text := []byte("<html><head><title>Page</title></head>") match := re.Find(text) fmt.Printf("First tag found: %s\n", match) // "<html>" }
The pattern matches opening HTML tags. Note that this is a simplified example and doesn't handle all HTML tag cases.
Finding Email Addresses
This example demonstrates finding email addresses in text. We use a basic email pattern for demonstration.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}`) text := []byte("Contact us at info@example.com or support@domain.org") match := re.Find(text) fmt.Printf("First email found: %s\n", match) // "info@example.com" }
The pattern matches common email formats. The method returns the first email address found in the text.
Finding URLs
This example shows how to find URLs in text. We use a simple pattern that matches common URL formats.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`https?://[^\s]+`) text := []byte("Visit https://example.com or http://test.org for more info") match := re.Find(text) fmt.Printf("First URL found: %s\n", match) // "https://example.com" }
The pattern matches URLs starting with http:// or https://. The method returns the first URL found in the input text.
Source
Go regexp package documentation
This tutorial covered the Regexp.Find
method in Go with
practical examples of pattern matching in byte slices.
Author
List all Go tutorials.