正则表达式

Simple regex

Regex quick reference
[abc]     A single character: a, b or c
[^abc]     Any single character but a, b, or c
[a-z]     Any single character in the range a-z
[a-zA-Z]     Any single character in the range a-z or A-Z
^     Start of line
$     End of line
\A     Start of string
\z     End of string
.     Any single character
\s     Any whitespace character
\S     Any non-whitespace character
\d     Any digit
\D     Any non-digit
\w     Any word character (letter, number, underscore)
\W     Any non-word character
\b     Any word boundary character
(...)     Capture everything enclosed
(a|b)     a or b
a?     Zero or one of a
a*     Zero or more of a
a+     One or more of a
a{3}     Exactly 3 of a
a{3,}     3 or more of a
a{3,6}     Between 3 and 6 of a

options: i case insensitive m make dot match newlines x ignore whitespace in regex o perform #{...} substitutions only once

正则表达式用法总结:
1. . 匹配除换行意外的任意字符
2. \w 匹配字母或数字或下划线或汉字
3. \s 匹配任意的空白字符
4. \d 匹配数字
5. \b 匹配单词的开始或结束
6. ^ 匹配字符串的开始
7. $ 匹配字符串的结束
8. * 重复零次或多次
9. + 重复一次或多次
10. 重复零次或一次
11. {n} 重复n次
12. {n,} 重复n次或更多
13. {n,m} 重复n次到m次


ip地址正则
$match = array();
$IP = "198.168.1.78";
preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',$IP, $match);
var_dump($match);


邮箱正则
$match = array();
$email = "test@ansoncheung.tk";
preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email, $match);
var_dump($match);


用户名正则
$match = array();
$username = "user_name12";
#preg_match('/^[a-z\d_]{5,20}$/i', $username, $match);
preg_match('/^[a-z]+[a-z0-9_]+$/i', $username, $match);
var_dump($match);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值