PHP Regular Expressions (With Operators and Examples)

By Shahnail Khan Last updated : December 19, 2023

What is a Regular Expression?

A regular expression, often abbreviated as regex or regexp, is a sequence of characters that defines a search pattern. This pattern can be simple, such as matching a specific character, or complex, involving a combination of characters that specify more intricate search criteria. When applied to text, regular expressions enable users to search for, match, and manipulate strings based on their defined patterns. They are widely used for tasks like text searching, validation, and substitution, providing a powerful and flexible tool for text processing operations.

Imagine you have a list of names, and you want to find everyone whose name starts with "J." However, the names might be written in various ways, like "John," "Jane," or "Jim."

A regular expression can help you express this pattern. For example, you might use the regular expression: ^J

This means you're looking for names that start (^) with the letter "J."

So, with this simple "code," you can easily pick out all the names in your list that begin with the letter "J," regardless of how they're spelt. Regular expressions provide a way to describe and find specific patterns in your data.

Regular expressions offer several advantages and find applications in various fields due to their powerful pattern-matching capabilities.

Regular Expressions Common Uses

Here are some advantages and common uses of regular expressions:

  1. Email Validation: Ensuring that email addresses follow a specific format.
  2. Phone Number Validation: Verifying that phone numbers are entered in a valid format.
  3. URL Parsing: Breaking down and extracting components from URLs.
  4. Log File Analysis: Filtering and extracting relevant information from log files.
  5. Data Extraction: Extracting specific information from structured or unstructured data.
  6. String Replacement: Replacing specific patterns with other text or characters.
  7. Password Strength Checking: Implementing rules for strong passwords using regular expressions.
  8. HTML Tag Matching: Parsing and manipulating HTML content by matching and modifying tags.
  9. Search and Replace Operations: Searching for specific patterns and replacing them with desired content.
  10. Form Validation: Checking user input in forms to ensure it meets specified criteria.

PHP Regular Expression Operators

Let's have a look at some common operators and metacharacters used in regular expressions along with examples:

  1. Literal Characters:
    • Example: abc
    • Matches the exact characters "abc" in the text.
  2. Dot (.) Operator:
    • Example: a.c
    • Matches any character between 'a' and 'c', such as "abc", "adc", etc.
  3. Asterisk (*) Operator:
    • Example: ab*c
    • Matches zero or more occurrences of the character 'b' between 'a' and 'c', such as "ac", "abc", "abbc", etc.
  4. Plus (+) Operator:
    • Example: ab+c
    • Matches one or more occurrences of the character 'b' between 'a' and 'c', such as "abc", "abbc", etc.
  5. Question Mark (?) Operator:
    • Example: ab?c
    • Matches zero or one occurrence of the character 'b' between 'a' and 'c', such as "ac", "abc", etc.
  6. Square Brackets ([]):
    • Example: [aeiou]
    • Matches any one of the vowels ('a', 'e', 'i', 'o', 'u').
  7. Caret (^) Inside Square Brackets:
    • Example: [^0-9]
    • Matches any character that is not a digit.
  8. Dollar ($) Anchor:
    • Example: abc$
    • Matches "abc" only if it appears at the end of the string.
  9. Backslash () Escape Character:
    • Example: \d
    • Escapes a metacharacter, treating it as a literal character. \d matches any digit.
  10. Parentheses (()):
    • Example: (abc)+
    • Groups multiple tokens together. (abc)+ matches one or more occurrences of "abc".
  11. Pipe (|) Alternation:
    • Example: cat|dog
    • Matches either "cat" or "dog".
  12. Quantifiers ({n}, {n,}, {n,m}):
    • Example: a{3}
    • Matches exactly three consecutive 'a' character.

PHP Regular Expression Special Character Class

  1. \n Newline:
    • Example: Line1\nLine2
    • Matches "Line1" followed by a newline character and then "Line2".
  2. \r Carriage Return:
    • Example: Before\rAfter
    • Matches "Before" followed by a carriage return character and then "After".
  3. \t Tab:
    • Example: Column1\tColumn2
    • Matches "Column1" followed by a tab character and then "Column2".
  4. \v Vertical Tab:
    • Example: Text\vNextLine
    • Matches "Text" followed by a vertical tab character and then "NextLine".
  5. \f Form Feed:
    • Example: Page1\fPage2
    • Matches "Page1" followed by a form feed character and then "Page2".
  6. \xhh Hexadecimal Character:
    • Example: \x41
    • Matches the ASCII character with the hexadecimal value 41, which is the uppercase letter 'A'.
  7. \xhh\xhh Hexadecimal Character Sequence:
    • Example: \x48\x65
    • Matches the ASCII characters with the hexadecimal values 48 (letter 'H') and 65 (letter 'e'). This would match "He" in a string.
  8. Combining Hexadecimal with Other Characters:
    • Example: A\x20B
    • Matches "A", followed by a space character (hexadecimal value 20), and then "B". This would match "A B" in a string.
  9. Using Hexadecimal for Control Characters:
    • Example: Start\x0D\x0AEnd
    • Matches "Start", followed by a carriage return (hexadecimal value 0D) and a line feed (hexadecimal value 0A), and then "End". This would match "Start" followed by a new line and "End".
  10. Hexadecimal Escape in Character Class:
    • Example: [\x41-\x5A]
    • Matches any uppercase letter between 'A' and 'Z'

PHP Regular Expression Quantifiers

  1. p+ (Matches any string with at least one 'p'):
    • Example: The regular expression "a+" would match strings like "apple," "grape," and "banana" because they all contain at least one 'a'.
  2. p (Matches any string with one or more 'p's):*
    • Example: The pattern "b*" would match strings like "berry," "grape," and "apple" because they either contain one or more 'b's.
  3. p? (Matches any string with zero or one 'p's):
    • Example: The expression "c?" would match strings like "cat," "dog," and "lion" because they either have zero or one 'c'.
  4. p{N} (Matches any string with a sequence of N 'p's):
    • Example: The regex "d{3}" would match strings like "dolphin" because it has exactly three consecutive 'd's.
  5. p{2,3} (Matches any string with a sequence of two or three 'p's):
    • Example: The pattern "e{2,3}" would match strings like "bee" and "see" because they contain either two or three consecutive 'e's.
  6. p{2, } (Matches any string with at least two 'p's):
    • Example: The regular expression "f{2,}" would match strings like "coffee" and "happiness" because they contain at least two consecutive 'f's.
  7. p$ (Matches any string with 'p' at the end):
    • Example: The expression "g$" would match strings like "song" and "sing" because they end with the character 'g'.
  8. ^p (Matches any string with 'p' at the start):
    • Example: The regex "^h" would match strings like "hello" and "high" because they start with the character 'h'.

PHP Regular Expression POSIX Function

  1. preg_match(): Searches for a pattern in a string and returns true if the pattern exists, otherwise returns false.
    • Example: The expression preg_match('/apple/', $fruit) would return true if the variable $fruit contains the word "apple."
  2. preg_match_all(): Matches all occurrences of a pattern in a string.
    • Example: The code preg_match_all('/\d+/', $text, $matches) would find all numeric sequences in the variable $text and store them in the array $matches.
  3. preg_replace(): Similar to ereg_replace(), but allows the use of regular expressions for search and replace.
    • Example: Using preg_replace('/\bword\b/', 'term', $sentence) would replace the whole word "word" with "term" in the variable $sentence.
  4. preg_split(): Divides a string by a regular expression, similar to the split() function.
    • Example: The code preg_split('/,/', $csv_data) would split a CSV string stored in $csv_data into an array based on commas.
  5. preg_grep():Finds elements in an array matching a regular expression pattern.
    • Example: Given an array $words, preg_grep('/^s/', $words) would return an array containing only the words starting with the letter 's'.
  6. preg_quote(): Quotes regular expression characters.
    • Example: If you want to search for the literal string "(test)" in a variable, you would use preg_match('/' . preg_quote('(test)') . '/', $content) to ensure proper matching without treating parentheses as special characters.

PHP Regular Expression: More Examples

Now let's take some examples to understand regular expressions in PHP better.

Example 1: Validating Email Addresses

Create a regular expression to validate whether a given string is a valid email address.

<?php
$email = "[email protected]";

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

if (preg_match($pattern, $email)) {
    echo "Valid email address";
} else {
    echo "Invalid email address";
}
?>

Output

Valid email address

Explanation

  • /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/: This regular expression breaks down as follows:
  • ^: Start of the line.
  • [a-zA-Z0-9._%+-]+: Match one or more of the allowed characters for the username part.
  • @: Match the at symbol.
  • [a-zA-Z0-9.-]+: Match one or more of the allowed characters for the domain name.
  • \.: Match the dot (.) before the top-level domain.
  • [a-zA-Z]{2,}: Match at least two alphabetic characters for the top-level domain.
  • $: End of the line.

Example 2: Extracting URLs from a Text

Extract all URLs from a given text using a regular expression.

<?php
$text =
    "Visit our website at https://example.com or check out our blog at http://blog.example.com.";

$pattern = "/https?:\/\/[a-zA-Z0-9-]+\.[a-zA-Z]{2,}(\S*)?/";

preg_match_all($pattern, $text, $matches);

echo "Found URLs:<br/>";
foreach ($matches[0] as $url) {
    echo $url . "<br/>";
}
?>

Output

Found URLs:
https://example.com
http://blog.example.com.

Explanation

  • /https?:\/\/[a-zA-Z0-9-]+\.[a-zA-Z]{2,}(\S*)?/: This regular expression can be broken down as follows:
    • https?: Match "http" or "https".
    • :\/\/: Match "://" literally.
    • [a-zA-Z0-9-]+: Match one or more of the allowed characters for the domain name.
    • \.: Match the dot (.) before the top-level domain.
    • [a-zA-Z]{2,}: Match at least two alphabetic characters for the top-level domain.
    • (\S*)?: Match any non-whitespace characters (optional) after the domain.

The preg_match_all() function is used to find all matches in the given text, and the results are then displayed.

Useful Links

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.