SoFunction
Updated on 2025-03-10

Solve doubts about regular expressions \w and \d

<?php
$string='April15,2003';
$pattern='/(\w+)(\d+),(\d+)/i';
$replacement='${1},${3}';
echo preg_replace($pattern,$replacement,$string);
?>

The result of this expression is April1,2003

Reason: (\w+) can fully match April15, but because there is (\d+) after (\w+), in order to match the defined rules, (\w+) will match April1, and (\d+) will match 5
You can change the expression to avoid this situation such as:

$pattern='/([a-z])(\d+),(\d+)/i';

The above is all the doubts about regular expressions \w and \d. Thank you for your support.