转换整数值区间,以正则表达式整数、区间、正则表达式

由网友(记忆昰座荒岛)分享简介:因此,我在寻找一个有关该问题的解决方案 - 如何转换整数区间为正则表达式。假设我有两个数字, A 和 B 。他们两人都是正整数,并让是 A<乙 I'm looking for a solution about the problem - how to convert integer interval to r...

因此,

我在寻找一个有关该问题的解决方案 - 如何转换整数区间为正则表达式。假设我有两个数字, A B 。他们两人都是正整数,并让是 A<乙

I'm looking for a solution about the problem - how to convert integer interval to regex. Suppose I have two numbers, A and B. Both of them are positive integers and let be A < B

现在,我正在寻找算法(可能是code),这将导致一个正则表达式,将匹配数字之间 A B (含边界)。例如,我有 A = 20 B = 35 ,那么正确的正则表达式是 ^ 2 [0-9] $ | ^ 3 [0-5] $ - 因为只有数字20..35将适合它

Now, I'm looking for algorithm (may be code) that will result in a single regex, that will match numbers between A and B (borders included). For example, I have A=20, B=35, then the correct regex is ^2[0-9]$|^3[0-5]$ - since only numbers 20..35 will fit to it.

在通常情况下,当 A 是像83724和 B 是这样28543485这不是那么明显,但是。

In common case, when A is something like 83724 and B is something like 28543485 it's not so obvious, however.

UPD。大多数情况下,它是一个的的好奇心的事情。 我知道做到这一点的最好办法:返回结果: A&LT; = X&功放;&安培; X - LT; = B

upd. Mostly, it is a curiosity matter. I know the best way to do this: return result: A<=X && X<=B

推荐答案

为什么在这种情况下使用正则表达式?

Why use regex in this situation?

我只想做到这一点:

布尔isBetween = NUM​​&GT; A和&放大器; NUM&LT; B:

(code用Java编写的)

(Code written in Java)

更容易,就像你问的可能是巨大的,在这种情况下使用它一个正则表达式是毫无意义的,效率低下。

Far easier, a regex like what you're asking for could be huge and using it in this situation would be pointless and inefficient.

好运。

如果你真的坚持使用正则表达式完成这个任务,请参见本网站,运行带有详细的正则表达式模式,它会向你解释笔者的正则表达式是如何工作的。

If you truly insist on using RegEx for this task, see this website, run the regex with verbose mode on and it will explain to you how the author's RegEx works.

阅读全文

相关推荐

最新文章