Regular Expressions (regexp)
The regexp package matches, finds, extracts, and replaces text using patterns — you compile a pattern once with regexp.MustCompile and reuse the resulting matcher everywhere.
Learn Regular Expressions (regexp) in our free Go course — an interactive lesson with runnable examples, a practice exercise and a quick reference.
Part of the free Go course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1️⃣ Compile, Match & Find
Start by compiling a pattern with regexp.MustCompile — written as a backtick raw string so the backslashes stay literal. The resulting *Regexp gives you MatchString (does it match at all?), FindString (the first match), and FindAllString (every match; pass -1 for no limit). Compile once and reuse — it's safe across goroutines.
2️⃣ Capture Groups & Named Groups
Wrap part of a pattern in parentheses to make a capture group . FindStringSubmatch then returns a slice where index 0 is the whole match and 1, 2, 3... are the groups in order. For readability, use named groups (?P<name>...) and look up a name's position with SubexpIndex — far clearer than counting parentheses.
3️⃣ Replacing & Validating
ReplaceAllString swaps every match for a replacement, and in that replacement $1 , $2 (or {'$ '} ) reference capture groups — perfect for reordering or reformatting. To validate a whole string, anchor the pattern with ^ and $ and call MatchString , so the pattern must cover the entire input.
🎯 Your Turn
Find every hashtag — a # followed by one or more word characters. Fill in the blank in the pattern.
Capture the area code and line number from every phone number with a pattern like (\d{3})-(\d{4}) (as a raw string). Use FindAllStringSubmatch and print each pair.
Practice quiz
Which function compiles a pattern and panics on an invalid one?
- regexp.Compile
- regexp.MustCompile
- regexp.New
- regexp.Build
Answer: regexp.MustCompile. regexp.MustCompile compiles the pattern and panics if it is invalid — ideal for package-level patterns.
Why are raw string literals (backticks) preferred for regex patterns?
- They run faster
- Backslashes need no escaping
- They allow newlines only
- They are required
Answer: Backslashes need no escaping. Raw strings avoid double-escaping backslashes, so stays readable.
What does re.MatchString(s) return?
- The matched text
- A bool: whether the pattern matches somewhere in s
- The match index
- A slice of matches
Answer: A bool: whether the pattern matches somewhere in s. MatchString reports a bool indicating whether the regexp matches any part of s.
What does re.FindString(s) return when there is a match?
- true
- The leftmost matched substring
- The whole string
- A slice
Answer: The leftmost matched substring. FindString returns the text of the leftmost match.
What does re.FindString(s) return when there is no match?
- nil
- An empty string
- A panic
- false
Answer: An empty string. With no match, FindString returns the empty string "".
What does re.FindAllString(s, -1) return?
- The first match
- A slice of all non-overlapping matches
- A count
- A bool
Answer: A slice of all non-overlapping matches. FindAllString with n=-1 returns a []string of all matches.
What does re.FindStringSubmatch return for a pattern with groups?
- Only group 1
- The whole match plus each capture group
- A bool
- Only the whole match
Answer: The whole match plus each capture group. Index 0 is the full match; subsequent indexes are the capture groups.
How do you write a named capture group in Go's regexp?
- (<name>...)
- (?P<name>...)
- (?:name...)
Answer: (?P<name>...). Go uses RE2's (?P<name>...) syntax for named groups.
Which method gives the index of a named group for use after FindStringSubmatch?
- GroupIndex
- SubexpIndex
- NameIndex
- MatchIndex
Answer: SubexpIndex. re.SubexpIndex("name") returns the position of that named group in the submatch slice.
In ReplaceAllString, how do you reference the first capture group?
- \1
- $1
- %1
- {1}
Answer: $1. Use $1 (or ${1}) to reference the first numbered capture group in the replacement.