REGEX接受以逗号分隔的数字,但数字范围为0-32767 [英] REGEX To accept numbers separated by commas, but number range is 0-32767
本文介绍了REGEX接受以逗号分隔的数字,但数字范围为0-32767的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要写一个正则表达式来采取这样的输入
I need to write a regular expression for taking input like this
23,456,22,1,32767
ie
- 开始或结束时不允许使用逗号。
- 空格可能会在之前和/或开始的逗号23,45,56,67等。
- 每个数字的范围应为0-32767。
目前我正在使用这样的正则表达式 [0-9] +(,[0-9] +)*
。
Currently I am using regular expression like this [0-9]+(,[0-9]+)*
.
这允许仅用逗号分隔数字(不允许空格),并且不检查数字的范围。
This allows for numbers separated by commas only ( not allowing spaces at all), and it does not check for the range of number.
推荐答案
这两个步骤可能是明智之举。首先检查范围是0-99999:
It's probably wise to do it in two steps. First check that the range is 0-99999:
^[0-9]{1,5}( *, *[0-9]{1,5})*$
然后将字符串解析为整数列表使用通用编程语言,并检查每个整数x x <= 32767
。
Then parse the string to a list of integers using a general purpose programming language and check that x <= 32767
for each integer x.
这篇关于REGEX接受以逗号分隔的数字,但数字范围为0-32767的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文