Non-capturing Parentheses

Sometimes we add sub-expressions as part of a larger expression but we don’t need the match data for that sub-expression. This is where non-capturing parentheses (aka. grouping-only parentheses) come in use. non-capturing parentheses prevent the sub-expression match being stored in the match array.

The syntax for non-capturing parenthese is: (?:…)


<?php

$string = '<tr><td>table data 1</td><td>table data 2</td></tr>';

$pattern = '/<tr><td>(.*?)<\/td><td>(.*?)<\/td><\/tr>/';

preg_match($pattern, $string, $match);

print_r($match);

?>

The above code snippet will print:

Array
(
[0] => <tr><td>table data 1</td><td>table data 2</td></tr>
[1] => table data 1
[2] => table data 2
)
Assuming that we only require data in the second <td> we can change the sub-expression in the first <td> into non-capturing as follows.


$pattern = '/<tr><td>(?:.*?)<\/td><td>(.*?)<\/td><\/tr>/';

and this will print:

Array
(
[0] => <tr><td>table data 1</td><td>table data 2</td></tr>
[1] => table data 2
)

It is said that the second method is more efficient and faster than the first (capturing everything) and also uses less memory (logical).

Leave a Comment