Regular Expressions (Regex) Tutorial: From Beginner to Practical Use
What Is a Regular Expression?
A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Regex engines can check whether a string matches a pattern, find all matches within a string, or replace matched portions with something else.
Regex is available in virtually every programming language: JavaScript, Python, PHP, Java, Ruby, Go, Rust — and in command-line tools like grep and sed.
While regex syntax can look intimidating at first glance (^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$), it follows strict, learnable rules. Let's build up from basics.
The Core Concepts
Literal Characters
The simplest regex is a literal string. The pattern hello matches any string containing the substring "hello." Case-sensitive by default.
Metacharacters (Characters with Special Meaning)
These characters have special meaning in regex: . * + ? ^ $ { } [ ] | ( ) \
To match these characters literally, escape them with a backslash: \. matches a literal period.
Character Classes
| Pattern | Matches |
|---|---|
[abc] | One character: a, b, or c |
[a-z] | Any lowercase letter |
[A-Z] | Any uppercase letter |
[0-9] | Any digit |
[^abc] | Any character except a, b, or c |
\d | Any digit (same as [0-9]) |
\D | Any non-digit |
\w | Word character: [a-zA-Z0-9_] |
\W | Non-word character |
\s | Whitespace (space, tab, newline) |
\S | Non-whitespace |
. | Any character except newline |
Quantifiers
| Quantifier | Meaning | Example |
|---|---|---|
* | 0 or more | a* matches "", "a", "aaa" |
+ | 1 or more | a+ matches "a", "aaa" (not "") |
? | 0 or 1 (optional) | colou?r matches "color" and "colour" |
{n} | Exactly n times | \d{4} matches exactly 4 digits |
{n,m} | Between n and m times | \d{2,4} matches 2–4 digits |
Anchors
| Anchor | Meaning |
|---|---|
^ | Start of string (or line in multiline mode) |
$ | End of string (or line in multiline mode) |
\b | Word boundary (position between \w and \W) |
Practical Regex Patterns You Can Use Today
Email Address Validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Matches a valid email address format. Note: regex cannot verify if an email actually exists — only if it's formatted correctly.
Indian Mobile Number
^[6-9]\d{9}$
Matches a 10-digit Indian mobile number starting with 6, 7, 8, or 9.
URL Validation (Basic)
^https?://[^\s/$.?#].[^\s]*$
Matches HTTP or HTTPS URLs.
Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
Matches dates in ISO 8601 format.
PIN Code (India)
^[1-9][0-9]{5}$
Matches a 6-digit Indian postal code not starting with 0.
Test Your Regex Patterns
Use Flixfer's free Regex Tester to test any regex pattern against your own input strings in real time — with match highlighting, group capturing, and flag options (case-insensitive, multiline, global). No installation required — runs entirely in your browser.