如何匹配正则表达式中的数值?
好吧,这是我自己遇到的一个非常有趣的挑战。
我的 RegEx 采用如下所示的输入行:
147.63.23.156/159 94.182.23.55/56 134.56.33.11/12
我需要它输出与所表示的范围匹配的正则表达式。让我解释一下。
例如,如果正则表达式接收到 147.63.23.156/159
,那么它需要输出与以下内容匹配的正则表达式:
147.63.23.156 147.63.23.157 147.63.23.158 147.63.23.159
我该怎么做?
目前我有:
(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})/(\d{1,3})
- $1 包含第一个
xxx.xxx.xxx.
部分 - $2 包含数字的下限
- $3 包含数字的上限
Okay, this is quite an interesting challenge I have got myself into.
My RegEx takes as input lines like the following:
147.63.23.156/159 94.182.23.55/56 134.56.33.11/12
I need it to output a regular expression that matches the range represented. Let me explain.
For example, if the RegEx receives 147.63.23.156/159
, then it needs to output a RegEx that matches the following:
147.63.23.156 147.63.23.157 147.63.23.158 147.63.23.159
How can I do this?
Currently I have:
(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})/(\d{1,3})
- $1 contains the first
xxx.xxx.xxx.
part - $2 contains the lower range for the number
- $3 contains the upper range for the number
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正则表达式确实不是验证 IP 地址的好方法,我想提前澄清这一点。解析地址并进行一些简单的算术来比较它们要容易得多。几个小于和大于就可以了。
也就是说,编写正则表达式生成器似乎是一个有趣的练习。我想出了一大堆 Python 代码来生成这些正则表达式。在展示代码之前,先看一下它为几个 IP 范围生成的正则表达式示例:
我将分两部分展示代码。首先,为简单整数范围生成正则表达式的部分。其次,处理完整IP地址的部分。
匹配数字范围
第一步是弄清楚如何生成匹配任意整数范围(例如 12-28 或 0-255)的正则表达式。这是我的实现中出现的正则表达式的示例:
现在是代码。有许多内联评论解释其背后的逻辑。总的来说,它依赖于大量的递归和特殊的大小写来尝试保持正则表达式的简洁和平均。
匹配 IP 地址范围
有了该功能,我编写了一个非常相似的 IP 范围函数来处理完整的 IP 地址。该代码与上面的代码非常相似,只是我们使用基数 256 而不是基数 10,并且代码抛出的是列表而不是字符串。
Regexes are really not a great way to validate IP addresses, I want to make that clear right up front. It is far, far easier to parse the addresses and do some simple arithmetic to compare them. A couple of less thans and greater thans and you're there.
That said, it seemed like it would be a fun exercise to write a regex generator. I came up with a big mess of Python code to generate these regexes. Before I show the code, here's a sample of the regexes it produces for a couple of IP ranges:
I'll show the code in two parts. First, the part that generates regexes for simple integer ranges. Second, the part that handles full IP addresses.
Matching number ranges
The first step is to figure out how to generate a regex that matches an arbitrary integer range, say 12-28 or 0-255. Here's an example of the regexes my implementation comes up with:
And now the code. There are numerous comments inline explaining the logic behind it. Overall it relies on a lot of recursion and special casing to try to keep the regexes lean and mean.
Matching IP address ranges
With that capability in place, I then wrote a very similar-looking IP range function to work with full IP addresses. The code is very similar to the code above except that we're working in base 256 instead of base 10, and the code throws around lists instead of strings.
如果您只需要一次构建一个,这个网站就可以解决问题。
如果您需要代码,并且不介意 python,此代码对任何任意数字范围执行此操作。
If you just need to build them one at at time, this website will do the trick.
If you need code, and don't mind python, this code does it for any arbitrary numeric range.
如果它是针对 Apache 的...我还没有尝试过,但它可能会工作:(
两个连续的
RewriteCond
由默认逻辑AND
连接)只需小心不同位数的范围(例如,95-105 应分为 95-99 和 100-105,因为它是字典顺序)。
If it's for Apache... I haven't tried it, but it might work:
(Two consecutive
RewriteCond
s are joined by a default logicalAND
)Just have to be careful with ranges with differing number of digits (e.g. 95-105 should be broken into 95-99 and 100-105, since it is lexicographic ordering).
我完全同意评论者的观点,纯正则表达式解决方案对于这里的工作来说是错误的工具。只需使用您已有的正则表达式提取前缀、最小值和最大值,
然后根据
${prefix}(\d+)
测试您的 IP 地址,并比较最后一组以查看是否符合在适当的范围内,
代码示例当然是伪代码 - 转换为您选择的语言。
I absolutely agree with the commenters, a pure-regex solution would be the wrong tool for the job here. Just use the regular expression you already have to extract the prefix, minimum, and maximum values,
then test your IP address against
${prefix}(\d+)
,and compare that last group to see if it falls within the proper range,
Code examples are pseudocode, of course - convert to your language of choice.
据我所知,这不能用直接的正则表达式来完成,但还需要一些背后的代码。例如,在 PHP 中,您可以使用以下内容:
为了与 RewriteCond 一起使用,我认为需要一些黑魔法......
无论如何,这将如何与 RewriteCond 一起使用?您是否拥有多台服务器并且想要轻松快速地制作 .htaccess 文件?如果是这样,那么只需将该函数添加到一个更大的脚本中,该脚本需要一些参数并生成 .htaccess 文件。
To my knowledge, this can't be done with straight up regex, but would also need some code behind it. For instance, in PHP you could use the following:
For this to work with a RewriteCond, I think some black magic would be in order...
How is this going to be used with RewriteCond, anyways? Do you have several servers and want to just quickly make a .htaccess file easily? If so, then just add that function to a bigger script that takes some arguments and burps out a .htaccess file.