使用正则表达式检查数字范围 [英] Checking a number range with regular expressions

查看:146
本文介绍了使用正则表达式检查数字范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用正则表达式来验证字符串中的某种格式。该字符串将成为游戏的规则。

I'm using a regular expression to validate a certain format in a string. This string will become a rule for a game.

示例:根据规则DX 3可以,但DX 14也可以正常...我知道如何查看字符串并找到一个或多个数字,所以问题是正则表达式也会匹配34,并且这个数字超出规则的范围......

Example: "DX 3" is OK according to the rule, but "DX 14" could be OK too... I know how to look at the string and find one or more "numbers", so the problem is that the regex will match 34 too, and this number is out of "range" for the rule...

我是否遗漏了有关正则表达式的事情?或者这根本不可能?

Am I missing something about the regex to do this? Or is this not possible at all?

推荐答案

不幸的是,在正则表达式中定义范围并不容易。如果你要使用1-23的范围你将得到这样的正则表达式:

Unfortunately there's no easy way to define ranges in regex. If you are to use the range 1-23 you'll end up with a regex like this:

([1-9]|1[0-9]|2[0-3])

说明:


  1. 值为1-9

  2. 或值以1开头,后跟0- 9

  3. 或者值以2开头,然后是0-3

这篇关于使用正则表达式检查数字范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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