Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2536741
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: 系统运维

2011-11-17 16:46:59

Regular expression is an art of the programing, it’s hard to debug , learn and understand, but the powerful features are still attract many developers to code regular expression. Let’s explore the following 10 practical regular expression ~ enjoy :)
1. Username Regular Expression Pattern
  1. ^[a-z0-9_-]{3,15}$
  2. ^           # Start of the line
  3. [a-z0-9_-]  # Match characters and symbols in the list, a-z, 0-9 , underscore , hyphen
  4. {3,15}      # Length at least 3 characters and maximum length of 15
  5. $           # End of the line

2. Password Regular Expression Pattern

  1. ((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})

  1. ( # Start of group
  2. (?=.*\d) # must contains one digit from 0-9
  3. (?=.*[a-z]) # must contains one lowercase characters
  4. (?=.*[A-Z]) # must contains one uppercase characters
  5. (?=.*[@#$%]) # must contains one special symbols in the list "@#$%"
  6. . # match anything with previous condition checking
  7. {6,20} # length at least 6 characters and maximum of 20
  8. ) # End of group
3. Hexadecimal Color Code Regular Expression Pattern
  1. ^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
  1. ^ #start of the line
  2. # # must constains a "#" symbols
  3. ( # start of group #1
  4. [A-Fa-f0-9]{6}  # any strings in the list, with length of 6
  5. | # ..or
  6. [A-Fa-f0-9]{3}  # any strings in the list, with length of 3
  7. ) # end of group #1
  8. $ #end of the line
4. Email Regular Expression Pattern
  1. ^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+
  2. (\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$
  1. ^ #start of the line
  2. [_A-Za-z0-9-]+ # must start with string in the bracket [ ], must contains one or more (+)
  3. ( # start of group #1
  4. \\.[_A-Za-z0-9-]+  # follow by a dot "." and string in the bracket [ ], must contains one o                     #r more (+)
  5. )* # end of group #1, this group is optional (*)
  6. @ # must contains a "@" symbol
  7. [A-Za-z0-9]+  # follow by string in the bracket [ ], must contains one or more (+)
  8. ( # start of group #2 - first level TLD checking
  9. \\.[A-Za-z0-9]+ # follow by a dot "." and string in the bracket [ ], must contains one or                 #more (+)
  10. )* # end of group #2, this group is optional (*)
  11. ( # start of group #3 - second level TLD checking
  12. \\.[A-Za-z]{2,} # follow by a dot "." and string in the bracket [ ], with minimum length o #f 2
  13. ) # end of group #3
  14. $ #end of the line

5. Image File Extension Regular Expression Pattern
  1. ([^\s]+(\.(?i)(jpg|png|gif|bmp))$)
  1. ( #Start of the group #1
  2. [^\s]+ # must contains one or more anything (except white space)
  3. ( # start of the group #2
  4. \. # follow by a dot "."
  5. (?i) # ignore the case sensitive checking
  6. ( # start of the group #3
  7. jpg # contains characters "jpg"
  8. | # ..or
  9. png # contains characters "png"
  10. | # ..or
  11. gif # contains characters "gif"
  12. | # ..or
  13. bmp # contains characters "bmp"
  14. ) # end of the group #3
  15. ) # end of the group #2
  16. $ # end of the string
  17. ) #end of the group #1
6. IP Address Regular Expression Pattern

  1. ^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.
  2. ([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$
  1. ^ #start of the line
  2. ( # start of group #1
  3. [01]?\\d\\d? # Can be one or two digits. 
  4.              #If three digits appear, it must start either 0 or 1
  5. # e.g ([0-9], [0-9][0-9],[0-1][0-9][0-9])
  6. | # ...or
  7. 2[0-4]\\d # start with 2, follow by 0-4 and end with any digit (2[0-4][0-9])
  8. | # ...or
  9. 25[0-5] # start with 2, follow by 5 and end with 0-5 (25[0-5])
  10. ) # end of group #2
  11. \. # follow by a dot "."
  12. .... # repeat with 3 time (3x)
  13. $ #end of the line



7. Time Format Regular Expression PatternTime in 12-Hour Format Regular Expression Pattern


  1. (1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm)
  1. ( #start of group #1
  2. 1[012] # start with 10, 11, 12
  3. | # or
  4. [1-9] # start with 1,2,...9
  5. ) #end of group #1
  6. : # follow by a semi colon (:)
  7. [0-5][0-9] # follow by 0..5 and 0..9, which means 00 to 59
  8. (\\s)? # follow by a white space (optional)
  9. (?i) # next checking is case insensitive
  10. (am|pm) # follow by am or pm
 Time in 24-Hour Format Regular Expression Pattern

  1. ([01]?[0-9]|2[0-3]):[0-5][0-9]
  1. ( #start of group #1
  2. [01]?[0-9] # start with 0-9,1-9,00-09,10-19
  3. | # or
  4. 2[0-3] # start with 20-23
  5. ) #end of group #1
  6. : # follow by a semi colon (:)
  7. [0-5][0-9] # follow by 0..5 and 0..9, which means 00 to 59

8. Date Format (dd/mm/yyyy) Regular Expression Pattern
  1. (0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)
  1. ( #start of group #1
  2. 0?[1-9] # 01-09 or 1-9
  3. | # ..or
  4. [12][0-9] # 10-19 or 20-29
  5. | # ..or
  6. 3[01] # 30, 31
  7. ) #end of group #1
  8. / # follow by a "/"
  9. ( # start of group #2
  10. 0?[1-9] # 01-09 or 1-9
  11. | # ..or
  12. 1[012] # 10,11,12
  13. ) # end of group #2
  14. / # follow by a "/"
  15. ( # start of group #3
  16. (19|20)\\d\\d # 19[0-9][0-9] or 20[0-9][0-9]
  17. ) # end of group #3

9. HTML tag Regular Expression Pattern

  1. <("[^"]*"|'[^']*'|[^'">])*>
  1. < #start with opening tag "<"
  2. ( # start of group #1
  3. "[^"]*" # only two double quotes are allow - "string"
  4. | # ..or
  5. '[^']*' # only two single quotes are allow - 'string'
  6. | # ..or
  7. [^'">] # cant contains one single quotes, double quotes and ">"
  8. ) # end of group #1
  9. * # 0 or more
  10. > #end with closing tag ">"
10. HTML links Regular Expression PatternHTML A tag Regular Expression Pattern
  1. (?i)]+)>(.+?)






阅读(833) | 评论(0) | 转发(0) |
0

上一篇:Java this and super

下一篇:pure css popup效果

给主人留下些什么吧!~~