What are best practices for effectively extracting specific text using regular expressions in PHP?

When using regular expressions in PHP to extract specific text from a string, it is important to use the preg_match function to match the pattern and extract the desired text. It is also recommended to use capturing groups in the regular expression pattern to isolate the specific text that needs to be extracted. Additionally, utilizing the preg_match_all function can be useful if there are multiple instances of the text that need to be extracted.

// Sample string containing specific text to extract
$string = "The price of the product is $50.00";

// Regular expression pattern to extract the price
$pattern = '/\$([\d.]+)/';

// Match the pattern and extract the price
if (preg_match($pattern, $string, $matches)) {
    $price = $matches[1];
    echo "Extracted price: $price";
} else {
    echo "Price not found";
}