Are there any best practices to follow when using regular expressions in PHP to extract numerical values with specific formats (e.g., including commas or periods)?

When using regular expressions in PHP to extract numerical values with specific formats, such as including commas or periods, it is important to consider the variations in formatting that may exist. One approach is to use a regular expression pattern that matches the desired numerical format while accounting for potential variations like commas or periods. Additionally, it may be helpful to use functions like `preg_replace()` to remove any unwanted characters before extracting the numerical value.

// Sample code to extract numerical values with commas or periods
$string = "The total amount is $1,234.56";
$pattern = '/[0-9,\.]+/';
preg_match($pattern, $string, $matches);
$numeric_value = str_replace(',', '', $matches[0]); // Remove commas
$numeric_value = floatval($numeric_value); // Convert to float if needed

echo $numeric_value; // Output: 1234.56