Regular Expressions for Beginners

Looking to test regular expressions online for free or finally understand how these mysterious character sequences work? Regular expressions (or “regex”) scare many developers. These cryptic patterns like ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ seem incomprehensible at first glance.

Yet, regex is a powerful tool for validating patterns, searching text, and extracting data. Whether you need to validate an email, a phone number, or extract information from a log, regular expressions are the solution.

Our regex tester allows you to experiment with flags and groups in real-time. It’s a regex tool for developers ideal for learning and debugging your patterns. In this progressive guide, discover how to master regex step by step.

What Is a Regular Expression?

A regex is a search pattern that describes a set of character strings. It allows you to:

  • Validate: Verify that an input matches a format (email, phone, etc.)
  • Search: Find occurrences in text
  • Extract: Capture specific parts of a string
  • Replace: Modify text according to patterns

Essential Basics

Literal Characters

By default, a character matches itself:

PatternMatches
hello”hello” in the text
2025”2025” in the text

Test in our regex tester: enter hello as pattern and hello world as text.

Special Characters (Metacharacters)

Certain characters have a special meaning:

CharacterMeaning
.Any character (except newline)
\dA digit (0-9)
\DAnything except a digit
\wA “word” character (letter, digit, underscore)
\WAnything except a word character
\sA whitespace (space, tab, newline)
\SAnything except whitespace

Examples:

  • \d\d\d matches “123”, “456”, “789”
  • \w\w\w matches “abc”, “A_1”, “foo”

Quantifiers

Quantifiers indicate how many times an element must appear:

QuantifierMeaning
*Zero or more
+One or more
?Zero or one (optional)
{n}Exactly n times
{n,}At least n times
{n,m}Between n and m times

Practical examples:

  • \d+: one or more digits → “1”, “123”, “99999”
  • \d{4}: exactly 4 digits → “2025”, “1234”
  • \d{2,4}: between 2 and 4 digits → “12”, “123”, “1234”

Character Classes

Brackets [] define a set of possible characters:

PatternMatches
[abc]”a”, “b” or “c”
[a-z]Any lowercase letter
[A-Z]Any uppercase letter
[0-9]Any digit (equivalent to \d)
[a-zA-Z]Any letter
[^abc]Anything BUT “a”, “b” or “c”

Examples:

  • [aeiou]: a vowel
  • [a-zA-Z0-9]: an alphanumeric character
  • [^0-9]: anything except a digit

Anchors

Anchors don’t match characters but positions:

AnchorMeaning
^Start of line/string
$End of line/string
\bWord boundary

Examples:

  • ^Hello : “Hello” at the beginning
  • end$ : “end” at the end
  • \bcat\b : the word “cat” (not “category”)

Groups and Alternatives

PatternMeaning
(abc)Capturing group
(?:abc)Non-capturing group
a|b”a” OR “b”

Examples:

  • (cat|dog): “cat” or “dog”
  • https?: “http” or “https” (the “s” is optional)

Practical Everyday Examples

Validate an Email

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Breakdown:

  • ^: start of string
  • [a-zA-Z0-9._%+-]+: local part (before @)
  • @: the @ character
  • [a-zA-Z0-9.-]+: domain name
  • \.: the dot (escaped because . is special)
  • [a-zA-Z]{2,}: extension (at least 2 letters)
  • $: end of string

Validate a French Phone Number

^(?:(?:\+|00)33|0)\s*[1-9](?:[\s.-]*\d{2}){4}$

Accepts: “0612345678”, “06 12 34 56 78”, “+33 6 12 34 56 78”

Extract URLs

https?://[^\s]+

Simple but effective for finding URLs in text.

Validate a French Postal Code

^[0-9]{5}$

5 digits exactly.

Find IP Addresses

\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b

Note: This finds IP formats, not necessarily valid IPs (0-255).

Validate a Strong Password

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

Requires: lowercase, uppercase, digit, special character, minimum 8 characters.

Need a strong password? Use our password generator!

Extract Hashtags

#\w+

Finds all hashtags in text.

Common Pitfalls to Avoid

1. Forgetting to Escape Special Characters

// ❌ Searches for anything.com
example.com

// ✅ Searches for exactly "example.com"
example\.com

Characters to escape: . * + ? ^ $ { } [ ] ( ) | \

2. Greedy Regex

Quantifiers * and + are “greedy” by default:

// Text: "<div>hello</div><div>world</div>"

// ❌ Greedy: captures everything between first < and last >
<.+>

// ✅ Non-greedy: captures each tag
<.+?>

3. Not Anchoring Validation Patterns

// ❌ Finds "123" in "abc123def"
\d+

// ✅ Validates that the entire string is a number
^\d+$

4. Case Sensitivity

// ❌ Doesn't find "HELLO"
hello

// ✅ With case-insensitive flag (i)
hello    // with /i
[Hh][Ee][Ll][Ll][Oo]  // without flag

Flags (Modifiers)

FlagMeaning
iCase insensitive
gGlobal (all occurrences)
mMultiline (^ and $ per line)
sThe dot . includes newlines

Our regex tester allows you to test different flags.

Regex in Different Languages

JavaScript

const regex = /\d+/g;
const text = "There are 3 apples and 5 oranges";

// Test
regex.test(text); // true

// Match
text.match(regex); // ["3", "5"]

// Replace
text.replace(/\d+/g, "X"); // "There are X apples and X oranges"

Python

import re

text = "There are 3 apples and 5 oranges"
pattern = r"\d+"

# Search
re.search(pattern, text)  # Match object for "3"

# All occurrences
re.findall(pattern, text)  # ["3", "5"]

# Replacement
re.sub(pattern, "X", text)  # "There are X apples and X oranges"

PHP

$text = "There are 3 apples and 5 oranges";
$pattern = "/\d+/";

// Search
preg_match($pattern, $text, $matches);

// All occurrences
preg_match_all($pattern, $text, $matches);

// Replacement
preg_replace($pattern, "X", $text);

Practical Exercises

Test these exercises in our regex tester:

Exercise 1: Dates

Find dates in DD/MM/YYYY format in: “Meeting on 15/03/2025 or on 20/04/2025”

Solution
\d{2}/\d{2}/\d{4}

Exercise 2: Euro Amounts

Extract prices in: “Item A: 19.99€ - Item B: 150€ - Total: 169.99 €“

Solution
\d+(?:,\d{2})?\s*

Exercise 3: Filenames

Find .jpg or .png files: “photo.jpg, document.pdf, image.png, video.mp4”

Solution
\w+\.(?:jpg|png)

Resources to Progress

Practice

  • Our regex tester with real-time feedback
  • regex101.com for detailed explanations
  • regexr.com with a complete reference

Memorization

  • The basics: ., \d, \w, \s
  • Quantifiers: *, +, ?, {n}
  • Anchors: ^, $, \b

Complementary Tools

To work efficiently with text:

Conclusion

Regular expressions are a powerful tool that deserves the learning investment. Start with the basics, practice regularly, and you’ll be surprised at how quickly you can build complex patterns.

The key: test, test, test. Our regex tester is here for that!

Don’t hesitate to come back to this guide as a reference, and happy practicing!