What are Regular Expressions?
Regular expressions (regex or regexp) are patterns used to match character combinations in strings. They are powerful tools for searching, validating, and manipulating text.
Common Regex Metacharacters
| Character | Description | Example |
|---|---|---|
| . | Any character | a.c → abc, aXc |
| * | 0 or more | ab* → a, ab, abb |
| + | 1 or more | ab+ → ab, abb |
| ? | 0 or 1 | colou?r → color, colour |
| ^ | Start of line | ^Hello |
| $ | End of line | world$ |
| \d | Digit | \d+ → 123 |
| \w | Word character | \w+ → hello_123 |
| \s | Whitespace | hello\sworld |
| [abc] | Character class | [aeiou] |
| () | Capture group | (ab)+ |
| | | Or | cat|dog |
Regex Flags
- g (global): Find all matches instead of stopping at the first
- i (case-insensitive): Ignore uppercase/lowercase differences
- m (multiline): ^ and $ match start/end of each line
- s (dotall): Dot (.) matches newlines too
Tips for Writing Regex
- Start simple and add complexity gradually
- Escape special characters with backslash: \. \* \?
- Use non-greedy quantifiers (*?, +?) when needed
- Test with edge cases and unexpected input