Regex Tester
Test regular expressions with live highlighting. Includes RE2 (Go) compatibility warnings.
Quick Reference
.any char (except \n)\d[0-9] digit\w[a-zA-Z0-9_]\swhitespace^start of string/line$end of string/line*0 or more+1 or more?0 or 1{n,m}n to m times(...)capture group(?:...)non-capture group(?P<n>...)named group (Go/RE2)[abc]character class[^abc]negated classa|ba or bWhat is a Regular Expression?
A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. It is used to match, find, validate, or extract parts of text. Regular expressions are built into virtually every programming language and are used daily for tasks like form validation, log parsing, data extraction, and find-and-replace in code editors.
For example, the pattern ^\d+$ matches any string containing only digits. The pattern [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]+ matches email addresses.
Different programming languages use different regex engines. JavaScript uses a PCRE-like engine that supports lookaheads and backreferences. Go uses the RE2 engine which guarantees linear-time matching but does not support lookaheads — an important difference when porting patterns between languages.
JavaScript vs Go RE2 Regex Differences
This tester uses the JavaScript regex engine. Key differences when using regex in Go (RE2):
| Feature | JavaScript | Go RE2 | Notes |
|---|---|---|---|
| Lookaheads (?= ?!) | Supported | NOT supported | Most common porting issue |
| Lookbehinds (?<= ?<!) | Supported | NOT supported | Use alternative patterns |
| Backreferences (\1) | Supported | NOT supported | RE2 is linear-time only |
| Named groups (?<name>...) | Supported | (?P<name>...) syntax | Go uses ?P<name> prefix |
| Unicode properties (\p{L}) | Supported | Supported | Both handle Unicode |
| Inline flags (?i:...) | Supported | Supported | Works in both |
| Non-greedy .*? | Supported | Supported | Same syntax |
| \d \w \s | Supported | Supported (ASCII only in RE2) | RE2 \d = [0-9] only |
Common Regex Patterns
| Pattern | Matches | Notes |
|---|---|---|
| ^[\w.+-]+@[\w-]+\.[\w.]{2,}$ | Email addresses | Basic validation, not RFC 5321 compliant |
| https?://[^\s]+ | URLs | Simple URL extraction |
| \b\d{4}-\d{2}-\d{2}\b | ISO dates (2024-01-15) | Works in RE2 and JS |
| \b(?:\d{1,3}\.){3}\d{1,3}\b | IPv4 addresses | Does not validate ranges |
| [a-fA-F0-9]{8}-(?:[a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12} | UUIDs | Works in all engines |
| (?P<year>\d{4})-(?P<month>\d{2}) | Named groups (Go syntax) | Use ?P<name> in Go |
Using Regex in Go
package main
import (
"fmt"
"regexp"
)
func main() {
// Compile once, reuse many times
re := regexp.MustCompile(`^[\w.+-]+@[\w-]+\.[\w.]{2,}$`)
// Test a string
fmt.Println(re.MatchString("user@example.com")) // true
// Find all matches
text := "contact a@b.com and c@d.org"
matches := re.FindAllString(text, -1)
fmt.Println(matches) // [a@b.com c@d.org]
// Named capture groups
dateRe := regexp.MustCompile(`(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})`)
names := dateRe.SubexpNames()
m := dateRe.FindStringSubmatch("Date: 2024-03-15")
for i, name := range names {
if name != "" && i < len(m) {
fmt.Printf("%s = %s\n", name, m[i])
}
}
}Common Use Cases
- ▸Form validation — Test email, phone, URL and postcode patterns before adding them to your codebase.
- ▸Log parsing — Build patterns to extract fields from structured log lines.
- ▸Go RE2 porting — Check if a PCRE pattern from Python or PHP will work unchanged in Go's regexp package.
- ▸Data extraction — Match and capture structured data from unstructured text like API responses or HTML.