正则表达式删除字母 H 及其后的任何数字

Regex to remove the letter H and any digits following it

请协助 PHP 中的正确正则表达式。我有一个字符串(线性格式化学式),我想在其中替换字母'H'(或)字母'H' 后跟数字。

例如:

CH3NO+       -> CNO+
C12H17ClN4OS -> C12ClN4OS
CNO3         -> CNO3
H2O          -> O
CHO          -> CO

这样做就可以了:

$newVariable = preg_replace('~H\d*~', '', $yourVariable);

\d*表示匹配数字为0个或多个则匹配

请使用它来解决您的问题。

<?php
$test = "CH3NO+";
echo preg_replace("/H\d+/i","",$test);
?>