Advanced Patterns in Regular Expressions - PHP Development
Introduction
Regular expressions (regex) are powerful tools for pattern matching and text manipulation. In PHP, regular expressions are commonly used to validate input, search for patterns, and replace text. This tutorial will cover advanced patterns in regular expressions, including lookaheads, lookbehinds, and conditionals.
Lookaheads
Lookaheads are used to assert that a certain pattern is followed by another pattern without including the second pattern in the match. There are two types of lookaheads: positive lookaheads and negative lookaheads.
Positive Lookahead
A positive lookahead (?=...)
asserts that what follows the current position in the string matches the pattern inside the lookahead. Here's an example:
Regex: /\d+(?= dollars)/
Code:
<?php $pattern = "/\d+(?= dollars)/"; $text = "I have 100 dollars."; preg_match($pattern, $text, $matches); print_r($matches); ?>
Output:
Array ( [0] => 100 )
Negative Lookahead
A negative lookahead (?!...)
asserts that what follows the current position in the string does not match the pattern inside the lookahead. Here's an example:
Regex: /\d+(?! dollars)/
Code:
<?php $pattern = "/\d+(?! dollars)/"; $text = "I have 100 dollars and 200 euros."; preg_match_all($pattern, $text, $matches); print_r($matches); ?>
Output:
Array ( [0] => Array ( [0] => 200 ) )
Lookbehinds
Lookbehinds are used to assert that a certain pattern is preceded by another pattern without including the preceding pattern in the match. There are two types of lookbehinds: positive lookbehinds and negative lookbehinds.
Positive Lookbehind
A positive lookbehind (?<=...)
asserts that what precedes the current position in the string matches the pattern inside the lookbehind. Here's an example:
Regex: /(?<=USD )\d+/
Code:
<?php $pattern = "/(?<=USD )\d+/"; $text = "The price is USD 100."; preg_match($pattern, $text, $matches); print_r($matches); ?>
Output:
Array ( [0] => 100 )
Negative Lookbehind
A negative lookbehind (?<!...)
asserts that what precedes the current position in the string does not match the pattern inside the lookbehind. Here's an example:
Regex: /(?<!USD )\d+/
Code:
<?php $pattern = "/(?<!USD )\d+/"; $text = "The price is USD 100 and 200 euros."; preg_match_all($pattern, $text, $matches); print_r($matches); ?>
Output:
Array ( [0] => Array ( [0] => 200 ) )
Conditionals
Conditionals in regular expressions allow you to apply different patterns based on whether a previous capturing group has been matched. The syntax for conditionals is (?(condition)yes-pattern|no-pattern)
. Here's an example:
Regex: /(a)?b(?(1)c|d)/
Code:
<?php $pattern = "/(a)?b(?(1)c|d)/"; $testStrings = ["abc", "bd"]; foreach ($testStrings as $text) { preg_match($pattern, $text, $matches); print_r($matches); } ?>
Output:
Array ( [0] => abc [1] => a ) Array ( [0] => bd )