9 min read
2026-03-08
(pattern) - group with capture (?:pattern) - group without capture (not saved) (?<name>pat) - named group
**Example - date parsing:**
(?<day>\\d{2})\\.(?<month>\\d{2})\\.(?<year>\\d{4})Corresponds to “12/25/2026”, groups: day=25, month=12, year=2026.
`\\1` refers to the first capture group:
(\\w+)\\s+\\1 - finds a repeating word: "the the"
| Syntax | Type | Meaning |
|---|---|---|
| `(?=pattern)` | Positive lookahead | Follows pattern |
| `(?!pattern)` | Negative lookahead | Should not pattern |
| `(?<=pattern)` | Positive lookbehind | Preceded by pattern |
| `(?<!pattern)` | Negative lookbehind | Not preceded by pattern |
**Example - price with currency sign:**
(?<=\\$)\\d+\\.?\\d* — number after the $ sign
**Email:**
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$**URL:**
https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b**Russian phone:**
^(\\+7|8)[\\s-]?\\(?[0-9]{3}\\)?[\\s-]?[0-9]{3}[\\s-]?[0-9]{2}[\\s-]?[0-9]{2}$**TIN (10 digits):**
^\\d{10}$**Atomic groups** `(?>pat)` - prevent rollback
**Lazy quantifiers** `*?`, `+?` - capture the minimum
**Avoid `.*`** in the middle of a pattern - use `[^"]+` for limited characters
**Anchors `^` and `$`** - speed up matching by limiting the search
Test regular expressions in real time in Regex Tester.
See also: Find and Replace, Data Extraction, JSON Formatter