从数值范围生成正则表达式
发布时间:2020-12-14 06:31:02 所属栏目:百科 来源:网络整理
导读:我想从数值范围生成(系列)regexp(s). 例: 1013 - 4044 = regexp matches---------------------------------------101[3-9] 1013 - 101910[2-9][0-9] 1020 - 109911[0-9][0-9] 1100 - 1199[23][0-9][0-9][0-9] 2000 - 399940[0-3][0-9] 4000 - 4039404[0-4]
|
我想从数值范围生成(系列)regexp(s).
例: 1013 - 4044 => regexp matches --------------------------------------- 101[3-9] 1013 - 1019 10[2-9][0-9] 1020 - 1099 11[0-9][0-9] 1100 - 1199 [23][0-9][0-9][0-9] 2000 - 3999 40[0-3][0-9] 4000 - 4039 404[0-4] 4040 - 4044 什么是最简单的算法? 扭转它的最简单方法是什么(即给出正则表达式,寻找范围)? 很高兴看到java,clojure,perl中的解决方案…… 谢谢!
给定范围有一个
online tool用于生成正则表达式,并提供解释.您也可以在那里找到源代码.例如:
^(101[3-9]|10[2-9][0-9]|1[1-9][0-9]{2}|[23][0-9]{3}|40[0-3][0-9]|404[0-4])$
First,break into equal length ranges:
1013 - 4044
Second,break into ranges that yield simple regexes:
1013 - 1019
1020 - 1099
1100 - 1999
2000 - 3999
4000 - 4039
4040 - 4044
Turn each range into a regex:
101[3-9]
10[2-9][0-9]
1[1-9][0-9]{2}
[23][0-9]{3}
40[0-3][0-9]
404[0-4]
Collapse adjacent powers of 10:
101[3-9]
10[2-9][0-9]
1[1-9][0-9]{2}
[23][0-9]{3}
40[0-3][0-9]
404[0-4]
Combining the regexes above yields:
(101[3-9]|10[2-9][0-9]|1[1-9][0-9]{2}|[23][0-9]{3}|40[0-3][0-9]|404[0-4])
Next we'll try factoring out common prefixes using a tree:
Parse into tree based on regex prefixes:
. 1 0 1 [3-9]
+ [2-9] [0-9]
+ [1-9] [0-9]{2}
+ [23] [0-9]{3}
+ 4 0 [0-3] [0-9]
+ 4 [0-4]
Turning the parse tree into a regex yields:
(1(0(1[3-9]|[2-9][0-9])|[1-9][0-9]{2})|[23][0-9]{3}|40([0-3][0-9]|4[0-4]))
We choose the shorter one as our result.
^(101[3-9]|10[2-9][0-9]|1[1-9][0-9]{2}|[23][0-9]{3}|40[0-3][0-9]|404[0-4])$
要反转它,您可以查看字符类,并获得每个替代项的最小值和最大值. ^(101[3-9]|10[2-9][0-9]|1[1-9][0-9]{2}|[23][0-9]{3}|40[0-3][0-9]|404[0-4])$
=> 1013 1020 1100 2000 4000 4040 lowers
1019 1999 1199 3999 4039 4044 uppers
=> 1013 - 4044
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
