2017-02-20 107 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. :("; 
    } 
相關問題