Regular expressions are powerful tools for pattern matching and text manipulation. This guide will help you master regex from basics to advanced techniques.
1. Basic Syntax
Regular expressions use special characters to define patterns:
- . - Matches any single character
- * - Matches zero or more of the preceding element
- + - Matches one or more of the preceding element
- ? - Matches zero or one of the preceding element
- ^ - Matches the start of a string
- $ - Matches the end of a string
2. Character Classes
Character classes let you match specific sets of characters:
- [abc] - Matches a, b, or c
- [^abc] - Matches anything except a, b, or c
- [a-z] - Matches any lowercase letter
- \d - Matches any digit (0-9)
- \w - Matches any word character
- \s - Matches any whitespace
Pro Tip
Use our Regex Tester to practice and debug your patterns in real-time!
3. Groups and Capturing
Parentheses create groups that can capture matched text:
- (abc) - Capturing group
- (?:abc) - Non-capturing group
- (?<name>abc) - Named capturing group
4. Common Patterns
Here are some frequently used regex patterns:
- Email:
[\w.-]+@[\w.-]+\.\w+ - URL:
https?://[\w.-]+(/[\w.-]*)* - Phone:
\d{3}[-.]?\d{3}[-.]?\d{4}