Complete Guide 8 min read

Regular Expressions (Regex): Complete Beginner Guide with Examples

Learn regex from scratch. Understand character classes, quantifiers, anchors, groups, and flags with practical examples.

regexregular expressionsregex tutorialregex examples

Regular Expressions: Beginner to Practical Guide

Regular expressions (regex) are patterns that describe text. They allow you to find, validate, extract, and replace text with precision that would take hundreds of lines of code otherwise. Every programming language and most text editors support regex.

Core Building Blocks

Literal characters match themselves: the pattern cat matches "cat" anywhere in a string.

The dot (.) matches any single character except newline: c.t matches "cat", "cut", "cot", "c2t".

Character classes in square brackets match any one character from a set: [aeiou] matches any vowel; [0-9] matches any digit; [a-z] matches any lowercase letter; [^aeiou] with a caret inside means NOT — matches any non-vowel.

Shorthand classes: \d matches any digit [0-9]; \D matches any non-digit; \w matches any word character [a-zA-Z0-9_]; \W matches any non-word character; \s matches any whitespace; \S matches any non-whitespace.

Quantifiers: How Many Times?

QuantifierMeaningExampleMatches

|---|---|---|---|

*Zero or morego*gle"ggle", "gogle", "google"
+One or morego+gle"gogle", "google" — not "ggle"
?Zero or one (optional)colou?r"color", "colour"
{3}Exactly 3\d{3}"123", "456"
{2,5}Between 2 and 5\d{2,5}"12", "123", "12345"
{2,}Two or more\d{2,}"12", "1234567890"

Anchors: Position in String

Anchors match positions, not characters.

The caret ^ matches the start of the string. The dollar sign $ matches the end. Word boundary  matches between a word character and a non-word character.

Critical difference: \d+ finds digits anywhere in a string, matching "123" inside "abc123def". But ^\d+$ requires the entire string to be digits — "abc123def" does not match.

Groups and Alternation

Parentheses create groups: (ab)+ matches "ab", "abab", "ababab". Groups can capture submatches for reuse.

Non-capturing groups (?:) group without capturing: (?:ab)+ matches the same but does not store the match.

Alternation with | means OR: cat|dog matches "cat" or "dog"; (JPG|PNG|WebP) matches any image extension.

Practical Patterns for India

Indian mobile number (10 digits starting with 6-9): ^[6-9]\d{9}$

Indian PIN code (6 digits, first not zero): ^[1-9][0-9]{5}$

PAN card (5 uppercase letters, 4 digits, 1 uppercase letter): ^[A-Z]{5}[0-9]{4}[A-Z]{1}$

Aadhaar number (12 digits, first digit 2-9): ^[2-9]{1}[0-9]{11}$

GST number (2 state digits + 10 PAN chars + entity + Z + checksum): ^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$

Email (practical, covers 99.9% of real cases): ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Date in DD/MM/YYYY: ^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/(19|20)\d{2}$

Flags

FlagSymbolEffect

|---|---|---|

GlobalgFind all matches, not just first
Case insensitivei[a-z] also matches [A-Z]
Multilinem^ and $ match line start/end
DotallsDot also matches newlines

Testing with Lazyblink

Open Regex Tester, enter your pattern, add flags as needed, type or paste test text. Matches highlight in real time as you type. See match count, captured groups, and positions. Copy the working pattern for your code. Lazyblink's regex tester is particularly useful for validating patterns against multiple test cases before using them in production code.

Frequently asked questions

Is regex the same in all languages?

Mostly similar, but there are differences between JavaScript, Python, PHP, and Java. Lazyblink uses JavaScript regex.

How do I match a literal dot?

Escape it with backslash: \. — unescaped, a dot matches any character.

Try this tool on Lazyblink

Put this guide into practice with our free online tool — no signup required.

Open tool