正则表达式的应用案例
Names
- Rules
- include a-z, A-Z
- Must start with A-Z
- No numbers
- No symbols
-
Regex
^(?!.*\s\s)(?!.*\.\.)(?!.*,,)[A-Z][a-zA-Z .,]{2,30}$ - Explaination
^$-> 开始于结束符号[A-Z][a-zA-Z .,]{2,30}-> 大写字母开头,后面包含大小写字母,.号,,号以及名字长度为{2,30}个字符- 增加过滤条件
(?!.*\s\s), 过滤掉有两个空格及以上的名字(?!.*\.\.), 过滤掉有两个.及以上的名字(?!.*,,), 过滤掉有两个,及以上的名字
Emails
- Rules
- include
a-z, A-Z - include
@
- include
- Regex
(?!.*\.\.)([\w.\-#!$%&'*+\/=?^_{}|~]{1,35})@([\w.\-]+)\.([a-zA-Z]{2,10}) - Explaination
([\w.\-#!$%&'*+\/=?^_{}|~]{1,35}),匹配@之前的可能字符,限定长度为{1,35}@符号([\w.\-]+), 匹配@之后,.之前可能的字符,可能有xxx@xx.xx的情况([a-zA-Z]{2,10}), 匹配.之后的字符,限定长度为{2,10}(?!.*\.\.), 过滤条件,过滤掉有..情况
URLs
- Rules
- Must be Valid
-
Regex
^(?:http|https|ftp):\/\/[a-zA-Z0-9_~:\-\/?#[\]@!$&'()*+,;=`^.%]+\.[a-zA-Z0-9_~:\-\/?#[\]@!$&'()*+,;=`^.%]+$ - Explaination
(?:http|https|ftp)—> 匹配三种中 的一种:\/\/[a-zA-Z0-9_~:\-\/?#[\]@!$&'()*+,;=^.%]+–> 匹配域名.–> 至少有一个.\/\/[a-zA-Z0-9_~:\-\/?#[\]@!$&'()*+,;=^.%]+` –> 匹配子域名
IP Address
- Rules
- 250 - 255 –>
25[0-5] - 200 - 249 –>
2[0-4][0-9] - 100 - 199 –>
1[0-9][0-9] - 000 - 099 –>
0?[0-9][0-9]?
- 250 - 255 –>
-
Regex
^(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9][0-9]?)$
Datas
- Rule
- Month/Day/Year
-
Regex
^(?:0?[1-9]|1[012])([\/\-])(?:0?[1-9]|1[0-9]|2[0-9]|3[01])\1(?:20[0-4][0-9]|2050)$ - Explaination
- Month –>
(0?[1-9]|1[0-2]]) - Day –>
(0?[1-9]|1[0-9])|2[0-9]|3[01]) - Year –>
(20[0-4][0-9]|2050), from 2000-2050
- Month –>
Time
- Rules
- 12 Hour Format
- Hour: 1-12
- Minutes: 0-59
- Seconds: 0-59
- 24 Hour Format
- Hour: 0-24
- Minutes: 0-59
- Seconds: 0-59
- 12 Hour Format
- Regex
- 12-hour format
^0?(?:[1-9]|1[012])\:(?:[0-5]?[0-9])(?:\:[0-5]?[0-9])?(?: am| pm| AM | PM)?$ - 24-hour format
^(?:0?[0-9]|1[0-9]|2[0-3]):(?:[0-5]?[0-9])(?::[0-5]?[0-9])?(?: GMT| EST)?$
- 12-hour format
Post Code
- Rules
- No Rule
- Regex
- Pakistan
^\d{5}$ - India
^\d{6}$ - CANADA
^[A-Z]\d[A-Z] \d[A-Z]\d$` - USA
^\d{5}(?:-\d{4})?$` - UK
^(?:[A-Z]{1,2}\d{1,2} \d[A-Z]{2})|(?:[A-Z]{1,2}\d[A-Z] \d[A-Z]{2})$`
- Pakistan
Credit Cards
- Rule
- Visa
- 16 digit
- Start with
4 4xxxxxxxxxxxxxxx4xxx-xxxx-xxxx-xxxx
- MasterCard
- 16 digit
- start with
51 to 55 51xxxxxxxxxxxxxx52xx-xxxx-xxxx-xxxx53xx-xxxx-xxxx-xxxx54xx-xxxx-xxxx-xxxx55xx-xxxx-xxxx-xxxx
- Discover
- 16 digit
- Start with
6011 6011xxxxxxxxxxxxx6011-xxxx-xxxx-xxxx
- American Express
- 15 digit
- start with
34 or 37 - 34xxxxxxxxxxxxx
- 37xx-xxxxxx-xxxxx
- China Union Pay
- 16 digit
- Start with
62 62xxxxxxxxxxxxxx62xx-xxxx-xxxx-xxxx
- Visa
- Regex
- Visia & MasterCard & Discover & Union Pay
^(4\d{3}|5[1-5]\d{2}|62\d{2}|6011)[ -]?(\d{4}[ -]?)(\d{4}[ -]?)(\d{4})$ - American Express
^(34|37\d{2})[ -]?(\d{6}[ -]?)(\d{5 - Combine tow
^(?:(?:4\d{3}|5[1-5]\d{2}|6011|62\d{2})([\- ]?)\d{4}\1\d{4}\1\d{4})|(?:(?:3[47])\d{2}([\- ]?)\d{6}\2\d{5})$ - Visia & MasterCard & Discover & Union Pay
Password
- Rule
- length 8 to 15
- At least 1 digit
- At least 1 a-z
- At least 1 A-Z
- At least 1 symbol
-
Regex
^(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[~!@#$%^&*{}\-[\];:<>'"?|]).{8,15}$ - Explaination
^.{8,15}$–> 匹配任意字符,限定长度(?=.*\d)–> 过滤器,匹配的字符中至少包含一个数字(?=.*[A-Z])(?=.*[a-z])–>过滤器,匹配的字符中至少包含一个大小写字母(?=.*[~!@#$%^&*{}\-[\];:<>'"?|])–>过滤器,匹配字符中至少包含一个符号