$re = '/\b(??px)(?<!(?<!\S)\.)(\d*)(\d)(?:\.(\d+))?px\b/m';
$str = 'font-size: 10px;
border: 0px;
margin-top: 4px; // dont touch
margin-bottom: 12px;
margin-right: 50%;
margin-left: -12px;
padding-left: 120px;
padding-right: 8px;
border-radius: calc(50% - 1px); // dont touch
grid-template-columns: 1fr 44px 1fr;
margin-top: 36.4px;
border-width: 10.5px;
border-width: .3px; // dont touch
border-width: .6px; // dont touch
border-width: 3.3px; // dont touch
border-width: 6.6px 4px 8000px .34px;
margin-left: -1px; // dont touch';
$subst = "$1.$2$3rem";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php