史上最全PHP正則表示式,如:郵箱、身份證、手機號、IP等
阿新 • • 發佈:2018-12-28
下面我用PHP語言來分別寫出各種正則表示式
下方程式碼直接複製即可
匹配郵箱
$str = '';
$isMatched = preg_match_all('/\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/', $str, $matches);
var_dump($isMatched, $matches);
匹配身份證
$str = ''; $isMatched = preg_match_all('/\d{17}[0-9Xx]|\d{15}/', $str, $matches); var_dump($isMatched, $matches);
匹配手機號
$str = '';
$isMatched = preg_match_all('/(13\d|14[579]|15[^4\D]|17[^49\D]|18\d)\d{8}/', $str, $matches);
var_dump($isMatched, $matches);
匹配IP(IPV4)
$str = '';
$isMatched = preg_match_all('/\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}/', $str, $matches);
var_dump($isMatched, $matches);
匹配中文
$str = ''; $isMatched = preg_match_all('/[\u4e00-\u9fa5]+/', $str, $matches); var_dump($isMatched, $matches);
匹配負整數
$str = '';
$isMatched = preg_match_all('/-[1-9]\d*/', $str, $matches);
var_dump($isMatched, $matches);
匹配正整數
$str = '';
$isMatched = preg_match_all('/[1-9]\d*/', $str, $matches);
var_dump($isMatched, $matches);
匹配日期(年-月-日)
$str = ''; $isMatched = preg_match_all('/(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)/', $str, $matches); var_dump($isMatched, $matches);
匹配時間(時:分:秒)
$str = '';
$isMatched = preg_match_all('/([01]?\d|2[0-3]):[0-5]?\d:[0-5]?\d/', $str, $matches);
var_dump($isMatched, $matches);