preg_match('/[a-z1-9]{2,5}-\d(\.\d)?/', "example.com - ABC-1.0", $match);
這是在http://gskinner.com/RegExr/工作。我在那裏得到預期的比賽 - 它匹配「ABC-1.0」。但不使用preg_match。 matches數組是空的。正則表達式在http://gskinner.com/RegExr/工作,但不與preg_match
preg_match('/[a-z1-9]{2,5}-\d(\.\d)?/', "example.com - ABC-1.0", $match);
這是在http://gskinner.com/RegExr/工作。我在那裏得到預期的比賽 - 它匹配「ABC-1.0」。但不使用preg_match。 matches數組是空的。正則表達式在http://gskinner.com/RegExr/工作,但不與preg_match
You need to use delimiters when using PCRE functions。您還需要正則表達式不區分大小寫。
preg_match('/[a-z1-9]{2,5}-\d(\.\d)?/i'
對,對不起。在代碼中有分隔符。 –
@MichelleWilliamson在不區分大小寫的情況下,'a'不會匹配'A'('/ i') –
您可以使用修飾符組添加不區分大小寫的正則表達式。
另請注意,組1是可選的,所以如果它沒有找到.number
組1將是空的。
/(?i)[a-z1-9]{2,5}-\d(\.\d)?/
如果你知道ABC
部分應該始終與大寫字母,您可以通過使用[A-Z1-9]
好過含糊明確的,當涉及到正則表達式,使其明確。
使用'$ preg_match_all()' –
[檢查'preg_match()'的文檔和示例。](http://php.net/manual/en/function.preg-match.php) – PeeHaa
可能的重複[使用preg_match函數的PHP錯誤](http://stackoverflow.com/questions/19114821) – cmbuckley