Regex Flavors Guide
Feature Comparison
| Feature | PCRE/PHP | Python re | JavaScript |
|---|---|---|---|
| Named groups | (?P<n>...) | (?P<n>...) | (?<n>...) |
| Lookbehind | (?<=...) | (?<=...) | ES2018+ |
| Recursive patterns | (?R) | No | No |
| Unicode props | \p{L} | regex pkg | \p{L} ES2018 |
| Atomic groups | (?>...) | No | No |
| Possessive quantifiers | *+, ++ | No | No |
| Inline flags | (?i) | (?i) | No |
| Dotall mode | (?s) or /s | re.DOTALL | /s ES2018 |
Named Groups
| Language | Syntax | Access |
|---|---|---|
| PCRE / PHP | (?P<year>\d{4}) | $matches['year'] |
| Python | (?P<year>\d{4}) | m.group('year') |
| JavaScript | (?<year>\d{4}) | m.groups.year |
| Go | (?P<year>\d{4}) | m[re.SubexpIndex("year")] |
Common Patterns
| Pattern | Regex |
|---|---|
^[^\s@]+@[^\s@]+\.[^\s@]+$ | |
| URL | https?://[^\s/$.?#].[^\s]* |
| IPv4 | (\d{1,3}\.){3}\d{1,3} |
| Date (YYYY-MM-DD) | \d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01]) |
| Hex color | #([0-9a-fA-F]{3}|[0-9a-fA-F]{6}) |
| UUID | [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} |