0Pricing
PHP Academy · Lesson

Regex Syntax Fundamentals

Learn character classes, quantifiers, anchors, and groups in PCRE.

Regex Syntax Fundamentals is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is a Regular Expression?

A regular expression (regex) is a pattern that describes a set of strings. PHP uses the PCRE (Perl-Compatible Regular Expressions) engine.

Delimiters

PHP regex patterns are enclosed in delimiters — commonly /, but you can also use # if the pattern contains slashes.

<?php
$pattern  = '/hello/i';   // case-insensitive
$pattern2 = '#https?://#'; // # avoids escaping /

Character Classes

Match one character from a set using square brackets: [aeiou], [a-z], [^0-9] (negated).

Shorthand Character Classes

Built-in shortcuts: \d digit, \w word char, \s whitespace, and their negations \D, \W, \S.

Anchors

^ matches start of string, $ matches end. Use them to validate full strings.

<?php
$pattern = '/^\d{5}$/'; // exactly 5 digits

Quantifiers

Specify repetitions: * (0+), + (1+), ? (0 or 1), {3} (exactly 3), {2,5} (2 to 5).

The Dot Metacharacter

. matches any character except newline. Escape as \. for a literal dot.

Capture Groups

Parentheses create a capture group — matched content available for extraction or backreferences.

<?php
$pattern = '/([A-Z]{2})(\d{4})/'; // captures letters + digits

Non-Capturing Groups

Use (?:...) to group without capturing — faster when you do not need the group value.

<?php
$pattern = '/(?:foo|bar)baz/'; // foobaz or barbaz

Alternation

The pipe | acts as OR — matches the left or right side.

<?php
$pattern = '/^(cat|dog|bird)$/';

Flags/Modifiers

Append after the closing delimiter: i (case-insensitive), m (multiline), s (dot matches newline), u (Unicode).

Greedy vs Lazy

Quantifiers are greedy by default (match as much as possible). Add ? to make lazy: .*? matches as little as possible.

Quick Check

Which shorthand matches any digit?

Frequently asked questions

Is the “Regex Syntax Fundamentals” lesson free?

Yes — the full text of “Regex Syntax Fundamentals” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Regex Syntax Fundamentals”?

Learn character classes, quantifiers, anchors, and groups in PCRE. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Regex Syntax Fundamentals” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Regex Syntax Fundamentals
  2. Matching with preg_match
  3. Replacing Text with preg_replace
  4. Splitting and Practical Patterns
← Back to PHP Academy