0

Given a string like "4>2", and in general "X>Y" is there a way to create a regular expression that accepts the string iff the condition is true?

4

2 回答 2

3

You couldn't. As a counter example, you have to be able accept

10 > 1

and generally

10^n > 10^m

for n > m. This would require counting, which normal regular expressions can't do. That said, if you have much more powerful regular expressions, as some languages do, you might be able to do this.

于 2013-06-02T05:20:36.587 回答
0

你可以,但它需要一些正则表达式(比如无符号短裤 0 .. 65535),数字不以 a 开头0

检查大小,如果

/^(\d{2}>\d{1})|(\d{3}>\d{2})|(\d{4}>\d{3})|(\d{5}>\d{4})$/

那么好的,否则检查相同的长度,如果

/^(\d{1}>\d{1})|(\d{2}>\d{2})|(\d{3}>\d{3})|(\d{4}>\d{4})|(\d{5}>\d{5})$/

then (same length) check digit by digit, if

/^(9.*?>[1-8])|(8.*?>[1-7])|(7.*?>[1-6])|(6.*?>[1-5])|(5.*?>[1-4])|(4.*?>[1-3])|
  (3.*?>[1-2])|(2.*?>1)/

else (2nd digit) if

/^(.9.*?>.[1-8])|(.8.*?>.[1-7])|(.7.*?>.[1-6])|(.6.*?>.[1-5])|(.5.*?>.[1-4])|(.4.*?>.[1-3])|
  (.3.*?>.[1-2])|(.2.*?>.1)/

 etc.. for 3rd to 5th digit
于 2013-06-02T05:37:13.163 回答