Post

Regular Expression

Regular Expression

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

1
abc   → matches "abc"

✔️ Metacharacters

SymbolMeaning
.any character
\ddigit
\wword character
\swhitespace

✔️ Quantifiers

PatternMeaning
*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

1
(abc)

✔️ Capture Example

1
(\w+)=(\d+)

Matches:

1
age=30
  • Group 1 → age
  • Group 2 → 30

5. Miscellaneous

✔️ Alternation

1
cat|dog

matches “cat” or “dog”

✔️ Greedy (default)
1
a.*b

matches longest possible

1
a123b456b → a [123b456] b
✔️ Lazy
1
a.*?b

matches shortest possible

1
a123b456b → a [123] b

meta characters

1
.  *  +  ?  ^  $  ( )  [ ]  { }  |  \

escape(‘\’) must need

1
\w+ → \\w+
FunctionDescription
regex_matchfull match
regex_searchpartial match
regex_replacereplace
regex_iteratoriterate matches

7. Useful Patterns

✔️ Email

1
2
3
\w+@\w+\.\w+

\\w+@\\w+\\.\\w+ (c++)

✔️ Number

1
\d+

✔️ Date (YYYY-MM-DD)

1
\d{4}-\d{2}-\d{2}

✔️ Phone

1
\d{3}-\d{3}-\d{4}
This post is licensed under CC BY 4.0 by the author.