Regular Expressions for Beginners: A Practical Guide With Free Regex Generator
Learn regular expressions from scratch with practical examples. Understand regex syntax, common patterns, and build expressions visually with our free regex generator.
What Are Regular Expressions?
Regular expressions (regex or regexp) are patterns used to match, search, and manipulate text. They are one of the most powerful tools in programming, used for input validation, text parsing, search and replace operations, data extraction, and log analysis.
Every major programming language supports regular expressions, and mastering them makes you significantly more productive as a developer.
Basic Regex Syntax
Literal Characters
The simplest regex is a literal string. The pattern "cat" matches the text "cat" exactly.
The Dot (.)
Matches any single character except a newline. The pattern "c.t" matches "cat", "cot", "cut", and "c9t".
Character Classes []
Match any one character from a set. [aeiou] matches any vowel. [0-9] matches any digit. [a-zA-Z] matches any letter.
Negated Character Classes [^]
Match any character NOT in the set. [^0-9] matches any non-digit character.
Quantifiers
Anchors
Escape Character (\)
Use backslash to match special characters literally. "\." matches an actual period. "\$" matches a dollar sign.
Common Regex Shorthand
Practical Regex Examples
Email Validation
Pattern: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Matches: [email protected], [email protected]
Phone Number (US)
Pattern: ^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
Matches: (555) 123-4567, 555-123-4567, 5551234567
URL Validation
Pattern: ^https?:\/\/[\w.-]+\.[a-zA-Z]{2,}[\/\w.-]*$
Matches: https://example.com, http://www.site.org/page
Date Format (MM/DD/YYYY)
Pattern: ^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$
Matches: 01/15/2026, 12/31/2026
Password Strength
Pattern: ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Requires: At least 8 characters, one uppercase, one lowercase, one digit, one special character
IP Address (IPv4)
Pattern: ^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
Matches: 192.168.1.1, 10.0.0.255
Build and test any regex pattern visually with our free Regex Generator.
Groups and Capturing
Parentheses ()
Groups part of a pattern together and captures the match. "(\d{3})-(\d{4})" captures the area code and number separately.
Non-Capturing Groups (?:)
Groups without capturing. "(?:abc)+" matches "abc" repeated but does not create a capture group.
Alternation |
Matches one pattern or another. "cat|dog" matches "cat" or "dog". "(Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day" matches any day of the week.
Lookahead and Lookbehind
Positive Lookahead (?=)
Matches a position followed by a specific pattern without including it in the match. "\d+(?= dollars)" matches the number in "100 dollars" but not in "100 euros".
Negative Lookahead (?!)
Matches a position NOT followed by a specific pattern. "\d+(?! dollars)" matches the number in "100 euros" but not in "100 dollars".
Positive Lookbehind (?<=)
Matches a position preceded by a specific pattern. "(?<=\$)\d+" matches the number after a dollar sign.
Negative Lookbehind (?<!)
Matches a position NOT preceded by a specific pattern.
Common Regex Mistakes
Free Developer Tools for Text and Code
Conclusion
Regular expressions are a powerful skill that every developer should learn. Start with the basics — literal characters, character classes, and quantifiers — then gradually add groups, lookaheads, and advanced patterns. Use our free Regex Generator to build, test, and debug your patterns visually before using them in code.
Try Our Free Tools
Generate passwords, QR codes, invoices, and 200+ more tools - completely free!
Explore All Tools