我发现此正则表达式为-2147483648到2147483647范围内的整数。
(0|[1-9]{1}[0-9]{0,8}|[1]{1}[0-9]{1,9}|[-]{1}[2]{1}([0]{1}[0-9]{8}|[1]{1}([0-3]{1}[0-9]{7}|[4]{1}([0-6]{1}[0-9]{6}|[7]{1}([0-3]{1}[0-9]{5}|[4]{1}([0-7]{1}[0-9]{4}|[8]{1}([0-2]{1}[0-9]{3}|[3]{1}([0-5]{1}[0-9]{2}|[6]{1}([0-3]{1}[0-9]{1}|[4]{1}[0-8]{1}))))))))|(\+)?[2]{1}([0]{1}[0-9]{8}|[1]{1}([0-3]{1}[0-9]{7}|[4]{1}([0-6]{1}[0-9]{6}|[7]{1}([0-3]{1}[0-9]{5}|[4]{1}([0-7]{1}[0-9]{4}|[8]{1}([0-2]{1}[0-9]{3}|[3]{1}([0-5]{1}[0-9]{2}|[6]{1}([0-3]{1}[0-9]{1}|[4]{1}[0-7]{1})))))))))
适用于-2147483648但不适用于2147483647,无论数字是什么,最后一位数都是问题.214748364有效......
答案 0 :(得分:2)
此'仅'匹配0 - 2147483647。
0 - 9
10 - 99
100 - 999
1000 - 9999
10000 - 99999
100000 - 999999
1000000 - 9999999
10000000 - 99999999
100000000 - 999999999
1000000000 - 2147483647
0 - 9
10 - 99
100 - 999
1000 - 9999
10000 - 99999
100000 - 999999
1000000 - 9999999
10000000 - 99999999
100000000 - 999999999
1000000000 - 1999999999
2000000000 - 2099999999
2100000000 - 2139999999
2140000000 - 2146999999
2147000000 - 2147399999
2147400000 - 2147479999
2147480000 - 2147482999
2147483000 - 2147483599
2147483600 - 2147483639
2147483640 - 2147483647
[0-9]
[1-9] [0-9]
[1-9] [0-9] {2}
[1-9] [0-9] {3}
[1-9] [0-9] {4}
[1-9] [0-9] {5}
[1-9] [0-9] {6}
[1-9] [0-9] {7}
[1-9] [0-9] {8}
1 [0-9] {9}
20 [0-9] {8}
21 [0-3] [0-9] {7}
214 [0-6] [0-9] {6}
2147 [0-3] [0-9] {5}
21474 [0-7] [0-9] {4}
214748 [0-2] [0-9] {3}
2147483 [0-5] [0-9] {2}
21474836 [0-3] [0-9]
214748364 [0-7]
[0-9] {1,9}
1 [0-9] {9}
20 [0-9] {8}
21 [0-3] [0-9] {7}
214 [0-6] [0-9] {6}
2147 [0-3] [0-9] {5}
21474 [0-7] [0-9] {4}
214748 [0-2] [0-9] {3}
2147483 [0-5] [0-9] {2}
21474836 [0-3] [0-9]
214748364 [0-7]
([0-9] {1,9} | 1 [0-9] {9} | 20 [0-9] {8} | 21 [0-3] [0-9] {7} | 214 [0-6] [0-9] {6} | 2147 [0-3] [0-9] {5} | 21474 [0-7] [0-9] {4} | 214748 [0-2] [0-9] {3} | 2147483 [0-5] [0-9] {2} | 21474836 [0-3] - [O- 9] | 214748364 [0-7])
。 [0-9] {1,9}
1 [0-9] {9}
2 0 [0-9] {8}
1 [0-3] [0-9] {7}
4 [0-6] [0-9] {6}
7 [0-3] [0-9] {5}
4 [0-7] [0-9] {4}
8 [0-2] [0-9] {3}
3 [0-5] [0-9] {2}
6 [0-3] [0-9]
([0-9] {1,9} | 1 [0-9] {9} | 2(0 [0-9] {8} | 1([0-3] [0-9] { 7} | 4([0-6] [0-9] {6} | 7([0-3] [0-9] {5} | 4([0-7] [0-9] {4} | 8([0-2] [0-9] {3} | 3([0-5] [0-9] {2} | 6([0-3] [0 -9] | 4 [0-7])))))))))
\ B([0-9] {1,9} | 1 [0-9] {9} | 2(0 [0-9] {8} | 1([0-3] [0-9 ] {7} | 4([0-6] [0-9] {6} | 7([0-3] [0-9] {5} | 4([0-7] [0-9] {4} | 8([0-2] [0-9] {3} | 3([0-5] [0-9] {2} | 6([0-3] [0 -9] | 4 [0-7])))))))))\ b'/ p>
答案 1 :(得分:1)
你可以用正则表达式来解决这个问题,但这真的不是正确的做法。这样的事情会更有效率:
function isValid(num)
{
if (num >= -2147483648 && num <= 2147483647)
return true;
else
return false
}
isValid(2147483646); //true
isValid(-2147483649); //false
答案 2 :(得分:1)
我正在回答这个问题,不是因为我认为用正则表达式做这个是个好主意,但是因为我可能在RegexMagic中发现了一个错误,同时让它为我构建这个怪物:
^(?:-?(?:214748364[0-7]|21474836[0-3][0-9]|2147483[0-5][0-9]{2}|214748[0-2][0-9]{3}|21474[0-7][0-9]{4}|2147[0-3][0-9]{5}|214[0-6][0-9]{6}|21[0-3][0-9]{7}|20[0-9]{8}|1[0-9]{9}|[1-9][0-9]{1,8}|[0-9])|-2147483648)$
或者,为“易读性”细分:
^
(?:
-?
(?:
214748364[0-7]
|
21474836[0-3][0-9]
|
2147483[0-5][0-9]{2}
|
214748[0-2][0-9]{3}
|
21474[0-7][0-9]{4}
|
2147[0-3][0-9]{5}
|
214[0-6][0-9]{6}
|
21[0-3][0-9]{7}
|
20[0-9]{8}
|
1[0-9]{9}
|
[1-9][0-9]{1,8}
|
[0-9]
)
|
-2147483648
)
$
答案 3 :(得分:0)
使用少数组来提高可读性:
(?:(?:-|\b)(?:1?[0-9]{1,9}|20[0-9]{8}|21[0-3][0-9]{7}|214[0-6][0-9]{6}|2147[0-3][0-9]{5}|21474[0-7][0-9]{4}|214748[0-2][0-9]{3}|2147483[0-5][0-9]{2}|21474836[0-3][0-9]|214748364[0-7])|-2147483648)\b
使用树状列表等组来加速搜索:
(?:(?:-|\b)(?:1?[0-9]{1,9}|20[0-9]{8}|21(?:[0-3][0-9]{7}|4(?:[0-6][0-9]{6}|7(?:[0-3][0-9]{5}|4(?:[0-7][0-9]{4}|8(?:[0-2][0-9]{3}|3(?:[0-5][0-9]{2}|6(?:[0-3][0-9]|4[0-7]))))))))|-2147483648)\b
显然,使用(?:-|\b)[0-9]{0,10}\b
然后尝试将结果解析为int32
是最有效的,但您可能无法始终访问解析器(示例文本编辑器搜索/替换)。 / p>