下面我用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);複製代碼
純原創,全部做品都是實戰經驗,但願能夠得到你們的支持。