问题描述
我有一个正则表达式 preg_match('/(?<=\$)\d+(\.\d+)?\b/', $str, $regs
返回我以货币表示的金额,但我正在尝试获取与金额相关的符号.
I have an Regex with me preg_match('/(?<=\$)\d+(\.\d+)?\b/', $str, $regs
which return me the amount in a currency, but what i am trying is to get the symbol associated with the amount too.
1) 例如字符串是 $300.00 要价
应该返回 $300.00 但现在它返回 300
2) 例如字符串是 EUR 300.00
应该返回 EUR300.00 但现在它返回 300
1) E.g. the string is $300.00 asking price
should return $300.00 but now it returns 300
2) E.g. the string is EUR 300.00
should return EUR300.00 but now it returns 300
只是我想要带金额的货币.
Simply i want the currency with amount.
谢谢
推荐答案
首先,您匹配可以是 $
或 EUR
的货币,然后是可选的空格:
First, you match the currency which can be either $
or EUR
, followed by optional white space:
(?:EUR|[$])\s*
然后,匹配主数字组,后跟可选句点和两位数字:
Then, match the main digit group, followed by an optional period and two digits:
\d+(?:\.\d{2})?
总的来说,我们得到了这个:
In total we get this:
$pattern = '/(?:EUR|[$])\s*\d+(?:\.\d{2})?/';
if (preg_match($pattern, $string, $matches)) {
echo $matches[0];
}
这篇关于正则表达式从字符串中获取货币和金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!