Golang Regexp.FindString
last modified April 20, 2025
This tutorial explains how to use the Regexp.FindString
method in Go.
We'll cover basic usage and provide practical examples of finding string matches.
A regular expression is a sequence of characters that defines a search pattern. It's used for pattern matching within strings.
The Regexp.FindString method returns the first match of the regular expression in the input string. It returns an empty string if no match is found.
Basic FindString Example
The simplest use of FindString
finds the first match of a pattern.
Here we search for a simple word in a string.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`hello`) match := re.FindString("hello there, hello world") fmt.Println(match) // "hello" noMatch := re.FindString("goodbye") fmt.Println(noMatch) // "" }
We compile the pattern "hello" and use FindString
to find the first
occurrence. It returns the matched substring or an empty string.
Finding Numbers in Text
This example demonstrates finding the first number in a string using FindString
.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`\d+`) text := "The price is 42 dollars and 99 cents" match := re.FindString(text) fmt.Printf("First number found: %s\n", match) // "42" }
The pattern \d+
matches one or more digits. FindString
returns only the first match in the input string.
Finding Email Addresses
We can use FindString
to extract the first email address from text.
package main import ( "fmt" "regexp" ) func main() { pattern := `[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}` re := regexp.MustCompile(pattern) text := "Contact us at support@example.com or sales@company.org" email := re.FindString(text) fmt.Println("First email found:", email) // "support@example.com" }
The email pattern matches standard email formats. FindString
stops
after finding the first match in the input text.
Case Insensitive Matching
This example shows how to perform case-insensitive matching with FindString
.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`(?i)hello`) match1 := re.FindString("Hello world") match2 := re.FindString("hElLo there") noMatch := re.FindString("Goodbye") fmt.Println(match1) // "Hello" fmt.Println(match2) // "hElLo" fmt.Println(noMatch) // "" }
The (?i)
flag makes the pattern case-insensitive. The method still
returns the original case of the matched substring.
Finding HTML Tags
This example demonstrates finding the first HTML tag in a string.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`<[^>]+>`) html := `<div>This is <b>bold</b> text</div>` tag := re.FindString(html) fmt.Println("First HTML tag:", tag) // "<div>" }
The pattern matches anything between angle brackets. FindString
returns the first complete HTML tag found in the input.
Finding Words Starting With Prefix
This example finds the first word starting with a specific prefix.
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`\bun\w*\b`) text := "The universe is unknown but not uninteresting" match := re.FindString(text) fmt.Println("First 'un' word:", match) // "universe" }
The pattern \bun\w*\b
matches whole words starting with "un".
FindString
returns the first such word found.
Finding First URL in Text
This example extracts the first URL from a block of text.
package main import ( "fmt" "regexp" ) func main() { pattern := `https?://[^\s]+` re := regexp.MustCompile(pattern) text := `Visit https://example.com or http://test.org for more info` url := re.FindString(text) fmt.Println("First URL found:", url) // "https://example.com" }
The pattern matches HTTP/HTTPS URLs. FindString
returns the first
complete URL found in the input text.
Source
Go regexp package documentation
This tutorial covered the Regexp.FindString
method in Go with
practical examples of finding string matches in text.
Author
List all Go tutorials.