java 正则表达式详解_你应该知道的10个java 正则表达式详解

正则表达式是编程的艺术,但正则表达式,不容易调试,也不好学习,但他强大的功能吸引了很多的开发者去使用正则表达式,本文总结了10个非常实用的正则表达式,并配备了详细的解释,这也是最好学习正则表达式的方法。

本文主要详细介绍如下正则表达式:

1. 用户名正则表达式

2. 密码正则表达式

3. 16进制颜色正则表达式

4. email 正则表达式

5. 文件扩展名 正则表达式

6. IP地址正则表达式

7. 时间正则表达式

8. 日期正则表达式

9. HTML 正则表达式

10. 超级链接正则表达式

1. 用户名正则表达式^[a-z0-9_-]{3,15}$

^ # Start of the line

[a-z0-9_-] # Match characters and symbols in the list, a-z, 0-9 , underscore , hyphen

{3,15} # Length at least 3 characters and maximum length of 15

$ # End of the line

2. 密码正则表达式

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

(# Start of group

(?=.*\d)# must contains one digit from 0-9

(?=.*[a-z])# must contains one lowercase characters

(?=.*[A-Z])# must contains one uppercase characters

(?=.*[@#$%])# must contains one special symbols in the list "@#$%"

.# match anything with previous condition checking

{6,20}# length at least 6 characters and maximum of 20

)# End of group

3. 16进制颜色正则表达式

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

^ #start of the line

# # must constains a "#" symbols

( # start of group #1

[A-Fa-f0-9]{6} # any strings in the list, with length of 6

| # ..or

[A-Fa-f0-9]{3} # any strings in the list, with length of 3

) # end of group #1

$ #end of the line

4. email 正则表达式

^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+

(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$

^#start of the line

[_A-Za-z0-9-]+# must start with string in the bracket [ ], must contains one or more (+)

(# start of group #1

\\.[_A-Za-z0-9-]+# follow by a dot "." and string in the bracket [ ], must contains one or more (+)

)*# end of group #1, this group is optional (*)

@# must contains a "@" symbol

[A-Za-z0-9]+ # follow by string in the bracket [ ], must contains one or more (+)

(# start of group #2 - first level TLD checking

\\.[A-Za-z0-9]+ # follow by a dot "." and string in the bracket [ ], must contains one or more (+)

)*# end of group #2, this group is optional (*)

(# start of group #3 - second level TLD checking

\\.[A-Za-z]{2,} # follow by a dot "." and string in the bracket [ ], with minimum length of 2

)# end of group #3

$#end of the line

5. 文件扩展名 正则表达式

([^\s]+(\.(?i)(jpg|png|gif|bmp))$)

(#Start of the group #1

[^\s]+# must contains one or more anything (except white space)

(# start of the group #2

\.#follow by a dot "."

(?i)#ignore the case sensitive checking

(# start of the group #3

jpg# contains characters "jpg"

|# ..or

png# contains characters "png"

|# ..or

gif# contains characters "gif"

|# ..or

bmp# contains characters "bmp"

)# end of the group #3

)# end of the group #2

$# end of the string

)#end of the group #1

6. IP地址正则表达式

^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.

([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$

^#start of the line

(# start of group #1

[01]?\\d\\d? # Can be one or two digits. If three digits appear, it must start either 0 or 1

# e.g ([0-9], [0-9][0-9],[0-1][0-9][0-9])

|# ...or

2[0-4]\\d# start with 2, follow by 0-4 and end with any digit (2[0-4][0-9])

| # ...or

25[0-5] # start with 2, follow by 5 and end with 0-5 (25[0-5])

)# end of group #2

\. # follow by a dot "."

.... # repeat with 3 time (3x)

$#end of the line

7. 时间正则表达式

12 小时制正则表达式

(1[012]|[1-9]):[0-5][0-9](\\s)?(?i)(am|pm)

(#start of group #1

1[012]# start with 10, 11, 12

|# or

[1-9]# start with 1,2,...9

)#end of group #1

:# follow by a semi colon (:)

[0-5][0-9]# follow by 0..5 and 0..9, which means 00 to 59

(\\s)?# follow by a white space (optional)

(?i)# next checking is case insensitive

(am|pm)# follow by am or pm

24 小时制正则表达式

([01]?[0-9]|2[0-3]):[0-5][0-9]

(#start of group #1

[01]?[0-9]# start with 0-9,1-9,00-09,10-19

|# or

2[0-3]# start with 20-23

)#end of group #1

:# follow by a semi colon (:)

[0-5][0-9]# follow by 0..5 and 0..9, which means 00 to 59

8. 日期正则表达式

(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)

(#start of group #1

0?[1-9]# 01-09 or 1-9

| # ..or

[12][0-9]# 10-19 or 20-29

|# ..or

3[01]# 30, 31

) #end of group #1

/# follow by a "/"

(# start of group #2

0?[1-9]#01-09 or 1-9

|#..or

1[012]#10,11,12

)# end of group #2

/#follow by a "/"

(# start of group #3

(19|20)\\d\\d# 19[0-9][0-9] or 20[0-9][0-9]

)# end of group #3

9. HTML 正则表达式

])*>

< #start with opening tag "]#cant contains one single quotes, double quotes and ">"

)# end of group #1

*# 0 or more

>#end with closing tag ">"

10. 超级链接正则表达式

\s* #can start with whitespace

(?i) # all checking are case insensive

href # follow by "href" word

\s*=\s* # allows spaces on either side of the equal sign,

( # start of group #1

"([^"]*") # only two double quotes are allow - "string"

| # ..or

'[^']*' # only two single quotes are allow - 'string'

| # ..or

([^'">]+) # cant contains one single / double quotes and ">"

) # end of group #1

有一点正则表达式的基础,然后通过这些正则表达式,慢慢分析,可以得到一般正则表达式的写法,而且可以学会很多规则,正则表达式的熟悉过程是个长期使用的过程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值