Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Basic Patterns in Regular Expressions

Introduction

Regular expressions (regex) are sequences of characters that form search patterns. They are used for pattern matching within strings. In PHP, regular expressions are utilized for searching, replacing, and extracting data from strings. Understanding basic patterns is the first step towards mastering regular expressions.

Literal Characters

Literal characters are the simplest form of patterns. They match the exact text in the target string.

Pattern: /cat/

Target String: "The cat sat on the mat."

Matches: "cat"

Metacharacters

Metacharacters are characters with special meanings in regular expressions. Some common metacharacters include:

  • . - Matches any single character except newline
  • ^ - Matches the start of the string
  • $ - Matches the end of the string
  • * - Matches 0 or more repetitions of the preceding element
  • + - Matches 1 or more repetitions of the preceding element
  • ? - Matches 0 or 1 repetition of the preceding element
  • [] - Matches any one of the characters inside the brackets

Pattern: /c.t/

Target String: "The cat sat on the mat."

Matches: "cat", "cat"

Character Classes

Character classes allow you to define a set of characters that you want to match. They are enclosed in square brackets [].

Pattern: /[aeiou]/

Target String: "The quick brown fox."

Matches: "e", "u", "i", "o", "o"

Predefined Character Classes

Predefined character classes are shorthand notations for commonly used character classes:

  • \d - Matches any digit, equivalent to [0-9]
  • \D - Matches any non-digit
  • \w - Matches any word character (alphanumeric + underscore)
  • \W - Matches any non-word character
  • \s - Matches any whitespace character (spaces, tabs, etc.)
  • \S - Matches any non-whitespace character

Pattern: /\d+/

Target String: "The price is 100 dollars."

Matches: "100"

Quantifiers

Quantifiers define the number of occurrences to match:

  • * - 0 or more
  • + - 1 or more
  • ? - 0 or 1
  • {n} - Exactly n times
  • {n,} - n or more times
  • {n,m} - Between n and m times

Pattern: /a{2,4}/

Target String: "aaaaa"

Matches: "aaaa"

Anchors

Anchors are used to position the regex at a specific location in the string:

  • ^ - Start of string
  • $ - End of string

Pattern: /^The/

Target String: "The quick brown fox."

Matches: "The"

Groups and Alternation

Groups are used to create subpatterns. Alternation allows for matching one of several patterns:

  • (...) - Grouping
  • | - Alternation (OR)

Pattern: /(cat|dog)/

Target String: "The cat and the dog."

Matches: "cat", "dog"

Examples in PHP

Here are some examples of how to use regular expressions in PHP:

Example 1: Matching a Simple Pattern

<?php
$pattern = "/cat/";
$text = "The cat sat on the mat.";
if (preg_match($pattern, $text)) {
    echo "Match found!";
} else {
    echo "No match found.";
}
?>
                    
Output: "Match found!"

Example 2: Extracting Data

<?php
$pattern = "/\d+/";
$text = "The price is 100 dollars.";
preg_match($pattern, $text, $matches);
echo "Found number: " . $matches[0];
?>
                    
Output: "Found number: 100"

Example 3: Replacing Text

<?php
$pattern = "/cat/";
$replacement = "dog";
$text = "The cat sat on the mat.";
$new_text = preg_replace($pattern, $replacement, $text);
echo $new_text;
?>
                    
Output: "The dog sat on the mat."