2017-02-20 110 views
-2

我有一个字符串:如何解决preg_match():未知修饰符'{'?

$11,950 Some random string $415 

,我想分析第一价格信息为整数:

11950 

为了获得与thouand分离器中的第一个数字的一​​部分,我创建了一个正则表达式:

[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)? 

online regex editor预期其工作。

然而,当我使用它的preg_match

$price = 0; 
$content = '$11,950 Some random string $415'; 
if (preg_match('[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)', $content, $matches)) {    
    var_dump($matches[0]); 
} 

我得到一个错误:

preg_match(): Unknown modifier '{' 

php sandbox

我在做什么错?

+3

'的preg_match('/ [0-9] {1,3}([0-9] {3})*(\ [0-9] + )/',$ content,$ matches)' – anubhava

+1

请阅读[分隔符](http://php.net/manual/en/regexp.reference.delimiters.php) –

回答

0

使用这样:

$regex = "/[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)/"; 
$content = '$11,950 Some random string $415'; 
    if (preg_match($regex, $content, $matches)) { 

     var_dump($matches[0]); 
    } else { 

     echo "The regex pattern does not match. :("; 
    } 
相关问题