Regular Expressions: A Beginner's Guide
Learn regex basics and how to use our regex tester to build and validate patterns for text processing.
FreeTools Dev Team
Text Processing Experts
Regular expressions (regex) are powerful tools for pattern matching and text manipulation. While they may seem intimidating at first, understanding regex basics can dramatically improve your text processing capabilities.
What Are Regular Expressions?
Regular expressions are sequences of characters that define search patterns. They're used for:
- Pattern Matching: Find specific text patterns in large documents
- Data Validation: Verify email addresses, phone numbers, and other formats
- Text Replacement: Replace multiple variations of text with a single command
- Data Extraction: Pull specific information from unstructured text
- Input Sanitization: Clean and validate user input
Quick Example: The regex pattern \d{3}-\d{3}-\d{4}
matches phone numbers like "123-456-7890".
Basic Regex Components
1. Literal Characters
Most characters in regex match themselves literally:
Pattern: hello
Pattern: 123
2. Character Classes
Character classes match specific types of characters:
Pattern | Description | Example Match |
---|---|---|
\d | Any digit (0-9) | 5, 0, 9 |
\w | Word character (a-z, A-Z, 0-9, _) | a, Z, 5, _ |
\s | Whitespace (space, tab, newline) | space, tab |
. | Any character (except newline) | a, 5, @, space |
[abc] | Any character in brackets | a, b, c |
[^abc] | Any character NOT in brackets | d, e, 1, @ |
3. Quantifiers
Quantifiers specify how many times a pattern should match:
Common Quantifiers
*
- 0 or more times+
- 1 or more times?
- 0 or 1 time (optional){n}
- Exactly n times{n,m}
- Between n and m times{n,}
- n or more times
Examples
\d+
matches "123", "5"\d{3}
matches "123" onlycolou?r
matches "color", "colour"\w{2,5}
matches "ab", "hello"a*
matches "", "a", "aaa"\d{3,}
matches "123", "12345"
Practical Regex Examples
Email Validation
Pattern: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
^
- Start of string[a-zA-Z0-9._%+-]+
- Username part (letters, numbers, some symbols)@
- Literal @ symbol[a-zA-Z0-9.-]+
- Domain name\.
- Literal dot (escaped)[a-zA-Z]{2,}
- Domain extension (2+ letters)$
- End of string✅ Valid Emails:
❌ Invalid Emails:
- userexample.com (missing @)
- [email protected] (missing domain)
- user@example (missing extension)
Phone Number Formats
US Phone Format
^\(\d{3}\) \d{3}-\d{4}$
Flexible Phone Format
^[\+]?[\d\s\-\(\)]{10,}$
URL Matching
Pattern: ^https?:\/\/[^\s]+$
^
- Start of stringhttps?
- "http" or "https" (? makes 's' optional):\/\/
- Literal "://"[^\s]+
- One or more non-whitespace characters$
- End of stringUsing Our Regex Tester
Our Regex Tester tool helps you build and validate regex patterns interactively:
- Enter your pattern: Type your regex in the pattern field
- Add test data: Input sample text to test against
- See live results: Matches are highlighted in real-time
- Test different flags: Global, case-insensitive, multiline options
- Get explanations: Understand what each part of your regex does
Regex Tester Features:
Advanced Regex Concepts
1. Anchors and Boundaries
Anchor | Description | Example |
---|---|---|
^ | Start of string/line | ^Hello matches lines starting with "Hello" |
$ | End of string/line | world$ matches lines ending with "world" |
\b | Word boundary | \bcat\b matches "cat" but not "catch" |
\B | Non-word boundary | \Bcat\B matches "cat" in "locate" |
2. Groups and Capturing
Capturing Groups: (\d{3})-(\d{3})-(\d{4})
3. Lookahead and Lookbehind
Positive Lookahead
\d+(?=px)
Negative Lookahead
\d+(?!px)
Common Regex Pitfalls
❌ Greedy vs. Lazy Matching
<.*>
matches the entire string<.*?>
matches each tag separately⚠ Escaping Special Characters
. ^ $ * + ? { } [ ] \ | ( )
3.14
matches "3X14" (. matches any character)3\.14
matches only "3.14"📱 Performance Considerations
(a+)+
.*
Regex in Different Languages
JavaScript
const regex = /\d+/g;
const text = "I have 5 cats and 3 dogs";
const matches = text.match(regex);
// Result: ["5", "3"]
Python
import re
regex = r'\d+'
text = "I have 5 cats and 3 dogs"
matches = re.findall(regex, text)
# Result: ['5', '3']
Pro Tip: Start simple and build complexity gradually. Test each part of your regex before combining them into more complex patterns.
Practice Exercises
Try These Patterns in Our Regex Tester:
Beginner:
- • Match all words starting with "cat"
- • Find all 4-digit years (1900-2099)
- • Extract hashtags from social media text
- • Validate simple passwords (8+ chars)
Advanced:
- • Match balanced parentheses
- • Extract CSS color values (#hex, rgb)
- • Validate credit card numbers
- • Parse log file timestamps
Conclusion
Regular expressions are powerful tools that can save hours of manual text processing. While the learning curve can be steep, mastering regex basics will make you significantly more efficient at handling text data.
Remember to start simple, test thoroughly, and use tools like our regex tester to build confidence with patterns before deploying them in production code.
Start Building Regex Patterns!
Use our tools to practice and perfect your regex skills