Go (Golang) Regex Tester
Test Go regular expressions online. Go uses RE2 syntax — no lookaheads, no backreferences. Paste your pattern and test string to see matches, groups, and ReplaceAllString output.
Test Go Regex Patterns Online
Go's regexp package uses RE2 syntax, which is deliberately more limited than PCRE (used by Python, JavaScript, PHP). Test your Go regex patterns here and see exact match behavior before writing code.
- RE2 syntax: Matches Go's
regexp.Compile()behavior exactly - FindAllString output: All non-overlapping matches
- FindStringSubmatch: Full match plus named and numbered capture groups
- ReplaceAllString preview: Test substitution patterns before using in code
Go RE2 vs. PCRE: Key Differences
- No lookaheads or lookbehinds:
(?=...)and(?<!...)are not supported — RE2 rejects patterns with lookarounds entirely - No backreferences:
\1referring to a previous capture group is not supported — RE2 can't match patterns like(\w+)\s\1(repeated word) - No possessive quantifiers:
a++and(?>...)atomic groups aren't supported - Named groups: Use
(?P<name>...)syntax (not(?<name>...)as in Python/JS) — access viaFindStringSubmatch+SubexpNames() - Guaranteed O(n) time: RE2 runs in linear time — no catastrophic backtracking regardless of input size, making it safe for use with untrusted input
Go regexp Quick Reference
- Compile once, use many times:
var re = regexp.MustCompile(`pattern`)at package level — compiling is expensive, matching is cheap - Raw string literals: Use backticks for regex patterns to avoid double-escaping backslashes:
`\d+`instead of"\\d+" - FindString vs. FindAllString:
FindStringreturns the first match;FindAllString(s, -1)returns all matches (-1= no limit) - Case-insensitive: Prefix pattern with
(?i)—(?i)hellomatches "Hello", "HELLO", "hello" - Multiline:
(?m)makes^and$match line boundaries;(?s)makes.match newlines
Frequently Asked Questions
Why doesn't my lookahead work in Go regex?
Go's regexp package implements RE2, which explicitly excludes lookaheads ((?=...), (?!...)) and lookbehinds ((?<=...), (?<!...)). This is a deliberate design decision — RE2 guarantees linear-time matching, which lookaheads break. If you need lookaheads, rethink the approach: often you can capture the surrounding context explicitly, use strings package functions for simple cases, or split matching into two steps (match broadly, then filter in Go code).
How do I do a case-insensitive match in Go?
Prefix your pattern with the inline flag (?i): regexp.MustCompile(`(?i)hello`) matches "hello", "Hello", "HELLO", and any mixed case. The flag applies to the entire expression unless scoped inside a group: (?i:hello) world makes only "hello" case-insensitive while "world" remains case-sensitive. There's no regexp.CompileInsensitive() function — the inline flag is the standard approach.
How do I extract named capture groups in Go?
Use (?P<name>...) for named groups, then access them via SubexpNames(): re := regexp.MustCompile(`(?P<year>\d{4})-(?P<month>\d{2})`)match := re.FindStringSubmatch(s)names := re.SubexpNames()
Then zip names and match into a map. The FindStringSubmatchIndex variant returns byte positions rather than substrings.