Java正则表达式匹配0-255的十进制数

问题

需要在Java中匹配输入为0-255的十进制数,使用正则表达式解决。

解决

\b(1?[0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b

解释如下:

"
\b(1?[0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b
"
g
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
1st Capturing Group (1?[0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
1st Alternative 1?[0-9]{1,2}
1 matches the character 1 with index 4910 (3116 or 618) literally (case sensitive)
Match a single character present in the list below [0-9]
{1,2} matches the previous token between 1 and 2 times, as many times as possible, giving back as needed (greedy)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
2nd Alternative 1[0-9][0-9]
1 matches the character 1 with index 4910 (3116 or 618) literally (case sensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
3rd Alternative 2[0-4][0-9]
2 matches the character 2 with index 5010 (3216 or 628) literally (case sensitive)
Match a single character present in the list below [0-4]
0-4 matches a single character in the range between 0 (index 48) and 4 (index 52) (case sensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
4th Alternative 25[0-5]
25 matches the characters 25 literally (case sensitive)
Match a single character present in the list below [0-5]
0-5 matches a single character in the range between 0 (index 48) and 5 (index 53) (case sensitive)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
Global pattern flags
g modifier: global. All matches (don't return after first match)
16-18	00

匹配例子如下:

00
01
99
255
100
150
175
200
250
0

参考

https://regex101.com/r/aAQ4bc/1

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值