如何匹配字符串格式" 20959WC-01"用PHP?

时间:2015-06-22 10:44:21

标签: php regex

我想限制用户输入一个类似于" 20959WC-01"的值。我的意思是它必须包含5个整数,然后是两个字符,a' - '和两个整数。

2 个答案:

答案 0 :(得分:2)

$pattern = '!
    ^                       # start of string
    \d{5}                   # five digits
    [[:alpha:]]{2}          # followed by two letters
    -                       # followed by a dash
    \d{2}                   # followed by two digits
    $                       # end of string
!x';
$matches = preg_match($pattern, $input);

答案 1 :(得分:0)

试试这个:

preg_match('/^[0-9]{3}$[A-Z]{2}-[0-9]{2}$/',$input);