How can the use of capturing groups in regular expressions impact the results of functions like preg_match_all in PHP?

When using capturing groups in regular expressions with functions like preg_match_all in PHP, the results returned will include both the full match as well as the captured groups. This can be useful for extracting specific parts of a string. To access only the captured groups, you can use the third parameter of preg_match_all to specify the flag PREG_SET_ORDER.

<?php
$string = "Hello, my email is john.doe@example.com";
$pattern = '/(\w+\.?\w+)@(\w+\.\w+)/';
preg_match_all($pattern, $string, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {
    echo "Full match: " . $match[0] . "\n";
    echo "Username: " . $match[1] . "\n";
    echo "Domain: " . $match[2] . "\n";
}
?>