currency regex在php中不起作用,其他地方则是


currency regex not working in php, elsewhere is

我有一个在regexr.com上运行的pregex /[A-Z]{3}'s'(.')/,但当我在php中与preg_match()一起使用时,它在USD ($)中返回false。我做错了什么?

编辑:我的php版本是5.4.32

if(preg_match('/[A-Z]{3}'s'(.')/g', $var)){
            return $var;
        }else{
           return 'Money error';
        }

这将返回"Money error",$var="USD($)";对我来说。

从regexr.com关闭regex之后有一个g属性,该属性不适用于php。

您不能将g放在正则表达式的末尾:

php > echo preg_match("/[A-Z]{3}'s'(.')/", 'USD ($)');
1

可以使用一些修饰符,但g不是其中之一。要进行全局搜索,请使用preg_match_all