⊗ppPmRgGL 229 of 447 menu

Limiting Greediness in Regex in PHP

Regular expressions are greedy by default. This means they capture the maximum possible number of characters.

Let's look at an example. Suppose we have the following string:

<?php $str = 'aeeex zzz x kkk'; ?>

Suppose we want to find the substring 'aeeex' in this string using the following pattern: the letter 'a', then any character one or more times, then the letter 'x'.

<?php $res = preg_replace('#a.+x#', '!', $str); ?>

We expect that the result in the variable will be the string '! zzz x kkk'. However, this is not the case - the variable contains the string '! kkk'.

The point is that our regex finds all characters from the letter 'a' to the letter 'x'. But our string has two letters 'x'. Due to greediness, the regex searches up to the very last 'x', thereby capturing not what we expected.

Of course, often this behavior is exactly what we need. But in this particular case, we would like to cancel the greediness and tell the regex to search up to the first 'x'.

To limit greediness, you need to put a question mark after the repetition operator:

<?php $res = preg_replace('#a.+?x#', '!', $str); ?>

Greediness can be limited for all repetition operators, like this: *?, ?? and {}?.

Given a string:

<?php $str = 'aba accca azzza wwwwa'; ?>

Write a regex that will find all strings bounded by the letter 'a', and replace each of them with '!'. Between the letters a there can be any character (except 'a').

byenru