REGEX 接受以逗号分隔的数字,但数字范围是 0-32767 [英] REGEX To accept numbers separated by commas, but number range is 0-32767

查看:21
本文介绍了REGEX 接受以逗号分隔的数字,但数字范围是 0-32767的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写一个正则表达式来接受这样的输入

I need to write a regular expression for taking input like this

23,456,22,1,32767

  1. 开头或结尾不允许使用逗号.
  2. 空格可以出现在逗号之前和/或逗号开头,例如23、45、56、67 等
  3. 每个数字的范围应为 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆