Regular Expressions
Prerequisites
1. What is a Regular Expressions
A Regular Expression (Regex) is a pattern used to match, search, or extract text. Instead of writing complex parsing logic, you describe patterns:
1
| "\\d+" → one or more digits
|
2. Why Use Regex?
- extract data (emails, numbers, logs)
- validate input (passwords, IDs)
- search patterns
- text processing
Use when:
- pattern is complex
- structure is unknown
- flexibility needed
Avoid when:
- simple parsing
- performance critical
- tight loops
The Regex processing is expensive
3. How to Use Regex?
✔️ Literals
| Symbol | Meaning |
|---|
. | any character |
\d | digit |
\w | word character |
\s | whitespace |
✔️ Quantifiers
| Pattern | Meaning |
|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 |
{n} | exactly n |
{n,m} | between n and m |
✔️ Examples
1
2
3
| \d+ → 123, 456
\w+ → hello, test123
a.*b → a___b
|
✔️ Character Classes
1
2
3
4
5
| [abc] → a or b or c
[^abc] → NOT a, b, c
[a-z] → lowercase letters
[A-Z] → uppercase letters
[0-9] → digits
|
✔️ Anchors
1
2
| ^ → start of string
$ → end of string
|
Example
1
2
| ^abc → starts with "abc"
xyz$ → ends with "xyz"
|
4. Groups and Captures
✔️ Grouping
✔️ Capture Example
Matches:
- Group 1 → age
- Group 2 → 30
5. Miscellaneous
✔️ Alternation
matches “cat” or “dog”
✔️ Greedy (default)
matches longest possible
1
| a123b456b → a [123b456] b
|
✔️ Lazy
matches shortest possible
1
| . * + ? ^ $ ( ) [ ] { } | \
|
escape(‘\’) must need
| Function | Description |
|---|
regex_match | full match |
regex_search | partial match |
regex_replace | replace |
regex_iterator | iterate matches |
7. Useful Patterns
✔️ Email
1
2
3
| \w+@\w+\.\w+
\\w+@\\w+\\.\\w+ (c++)
|
✔️ Number
✔️ Date (YYYY-MM-DD)
✔️ Phone