如何在PHP正则表达式中匹配两个单词后得到整数或浮点值?
我有一个带整数和浮点值的字符串。现在我想通过匹配两个单词来提取值 我的字符串看起来像下面的文本。每个字符串都有余额,即文本。现在我想通过PHP正则表达式得到余额如何在PHP正则表达式中匹配两个单词后得到整数或浮点值?,php,regex,Php,Regex,我有一个带整数和浮点值的字符串。现在我想通过匹配两个单词来提取值 我的字符串看起来像下面的文本。每个字符串都有余额,即文本。现在我想通过PHP正则表达式得到余额 $str_demo_one = "210:Recharge request of TK 200 for mobile no. 1789000000, transaction ID BD07232231230125 is successful. Your account balance is TK 13769.98."; $str_d
$str_demo_one = "210:Recharge request of TK 200 for mobile no. 1789000000, transaction ID BD07232231230125 is successful. Your account balance is TK 13769.98.";
$str_demo_two = "563:Recharge 150 Tk to 1880000000 is successful. Transaction number is R190723.2158.6600eb.Your new balance is tk 3852.55 TAKA. Thank You.";
$str_demo_three = "Recharge Request of TK 300 for mobile no 1942300468, transaction ID R190723.2152.2201e1 is successful. your account balance is TK 5434.72.";
$str_demo_four = "Recharge 650 Tk to 16300000 is successful. Transaction number is R190723.1106.710166.Your new balance is 5921.18 TAKA. Thank You.";
$str_demo_five = "Transaction ID is 0718140110124179. 1531141036 has been recharged successfully with 89 Taka. Your new balance is 2529 Taka.";
我试过使用流动代码
preg_match_all('/(余额为)(\D*)(\D+)/i',$str,$matches)代码>
它只返回整数值,不返回浮点值。使用以下正则表达式
余额是(?:t(?:k | aka))?\s*(\d+(?:\。\d+)/ig
如果装置为“tk”或“taka”,则此功能将起作用
注意,/i
标志用于不区分大小写的匹配
在您的模式中,您没有考虑小数部分。您可以将模式更新为(余额为)(\D*)(\D+(?:\。\D+))
如果您只需要匹配,可以使用\K来忘记匹配的内容,省略捕获组并使小数部分可选
\bbalance is \D*\K\d+(?:\.\d+)?
向我们展示您为实现相同目标所做的尝试。preg_match_all('/(余额为)(\D*)(\D+)/i',$str,$matches);请编辑问题以包含相关详细信息,而不是将其作为评论发布