用于接受有效主机名、IPv4 或 IPv6 地址的 Java 正则表达式
有人有一个好的(最好是经过测试的)正则表达式来仅接受有效的 DNS 主机名、IPv4 或 IPv6 地址吗?
Does anyone have a good (preferably tested) regex for accepting only a valid DNS hostname, IPv4, or IPv6 address?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我了解您可能被迫使用正则表达式。但是,如果可能的话,最好避免使用正则表达式来执行此任务,而是使用 Java 库类来进行验证。
如果您想同时进行验证和 DNS 查找,则
InetAddress.getByName(String)
是一个不错的选择。这将一次性处理 DNS、IPv4 和 IPv6,并返回一个整齐包装的InetAddress
实例,其中包含 DNS 名称(如果提供)和 IPv4 或 IPv6 地址。如果您只想进行语法验证,那么 Apache commons 有几个类可以完成这项工作:
DomainValidator
和InetAddressValidator
。I understand that you may be forced to use a regex. However, if possible it is better to avoid using regexes for this task and use a Java library class to do the validation instead.
If you want to do validation and DNS lookup together, then
InetAddress.getByName(String)
is a good choice. This will cope with DNS, IPv4 and IPv6 in one go, and it returns you a neatly wrappedInetAddress
instance that contains both the DNS name (if provided) and the IPv4 or IPv6 address.If you just want to do a syntactic validation, then Apache commons has a couple of classes that should do the job:
DomainValidator
andInetAddressValidator
.Guava 有一个新类 HostSpecifier。它甚至会根据最新的 mozilla 公共后缀验证主机名(如果是主机名)是否以有效的“公共后缀”(例如“.com”、“.co.uk”等)结尾列表。这是您不想用手工制作的正则表达式尝试的事情!
Guava has a new class HostSpecifier. It will even validate that the host name (if it is a host name) ends in a valid "public suffix" (e.g., ".com", ".co.uk", etc.), based on the latest mozilla public suffix list. That's something you would NOT want to attempt with a hand-crafted regex!
正如其他人所说,使用正则表达式执行此操作是一项相当大的挑战,而且不可取。但使用 IPAddress Java 库 可以轻松解析主机名、IPv4 和 IPv6 地址,不触发 DNS 查找。免责声明:我是该图书馆的项目经理。
示例代码:
输出:
As others have said, doing this with a regex is quite a challenge and not advisable. But it is easy to do with the IPAddress Java library which can parse host names, IPv4 and IPv6 addresses, without triggering DNS lookup. Disclaimer: I am the project manager of that library.
Sample code:
Output:
受到我在这篇文章,我创建了以下验证器方法,它似乎非常适合简单的验证需求。通过阅读 URI 的 JavaDoc,我删除了一些误报,例如“host:80”和“主机名/页面”,但我不能保证还剩下一些误报。
Inspired by the code I found in this post, I created the following validator method that seems to suit simple validation needs quite nicely. By reading the JavaDoc of URI I removed some false positives such as "host:80" and "hostname/page", but I cannot guarantee there are some false positives left.
你也可以这样做。比方说:
You can also do this. Let's say: