Regular Expressions Guide
What are Regular Expressions?
Regular expressions (regex) are patterns used to match character combinations in strings. They are a powerful tool for searching, validating, and transforming text across almost every programming language.
Essential Syntax
| Pattern | Meaning | Example |
|---|---|---|
| . | Any character | a.c → abc, axc |
| * | Zero or more | ab* → a, ab, abb |
| + | One or more | ab+ → ab, abb |
| ? | Optional | colou?r → color, colour |
| \d | Digit | \d+ → 123 |
| \w | Word character | \w+ → hello_world |
| ^ | Start of string | ^Hello |
| $ | End of string | world$ |
Common Patterns
Email Validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$URL Match
https?://[^\s]+IP Address
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\bCommon Mistakes
- Greedy matching: Use
.*?instead of.*for non-greedy. - Not escaping special characters: Use
\.to match a literal dot. - Forgetting anchors: Without
^and$, a pattern matches substrings.
Test your patterns with our Regex Tester, see matches highlighted in real time.
FAQ
Are regex the same across languages?
Core syntax is similar, but flavors (PCRE, JavaScript, Python) have differences in lookaheads, named groups, and unicode support.
When should I avoid regex?
Don't use regex to parse HTML, XML, or nested structures. Use a proper parser instead.