正则表达式接受以逗号分隔的数字和/或数字范围,但范围在 1-4093 之间 [英] Regex to accept numbers and/or number range separated by commas, but between range 1-4093
本文介绍了正则表达式接受以逗号分隔的数字和/或数字范围,但范围在 1-4093 之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个正则表达式来验证用户输入的 VLAN 字符串.字符串应允许数字或范围,以逗号分隔.数字必须介于 1 到 4093 之间.
I need a regex to validate VLAN string entered by user. The string should allow numbers or ranges, separated by comma. The numbers must be between 1 and 4093.
允许以下样品:
1,
1,2,3,4
1-10,
1-4093
4000
我在下面试过:
^0*([1-9]|[1-8][0-9]|9[0-9]|[1-8][0-9]{2}|9[0-8][0-9]|99[0-9]|[1-3][0-9]{3}|40[0-8][0-9]|409[0-3])$
需要增强逗号分隔和范围
Need to enhance for comma separated and ranges
推荐答案
要匹配从 1 到 4093 的数字,可以使用:
To match a number from 1 to 4093 one can use:
(?:[1-9]\d{0,2}|[1-3]\d{3}|40(?:[0-8]\d|9[0-3]))
我们将称之为 N
.现在是重复部分:
That we'll call N
. Now the repetition part:
^(N)(?:[,-] *(N)?)*$
给出:
^(?:[1-9]\d{0,2}|[1-3]\d{3}|40(?:[0-8]\d|9[0-3]))(?:[,-] *(?:[1-9]\d{0,2}|[1-3]\d{3}|40(?:[0-8]\d|9[0-3]))?)*$
这篇关于正则表达式接受以逗号分隔的数字和/或数字范围,但范围在 1-4093 之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文