简体   繁体   English

在Rails中使用正则表达式验证域名和IP地址

[英]Validate domain name and ip address using regex in Rails

I have tried build app like whois domain tool site. 我尝试过构建类似whois域工具站点的应用程序。

On form have one text_field and I want field only input with domain name and ip address. 在表单上有一个text_field ,我只想输入带有域名和IP地址的字段。

Example domain name : domain.com (without http://www. or https://www. or subdomain. ) 域名示例:domain.com(不带http://www.https://www.subdomain.

Example Ip address : 4.4.4.4 范例IP地址:4.4.4.4

I want validate using regex, I have create this regex ( demo on rubular ) 我想使用正则表达式进行验证,我已经创建了这个正则表达式( 在rubular上演示

([a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+.*)

But that is not clean, www. 但这不干净,www。 or subdomain still accepted. 或子域仍被接受。

IP地址:

/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}(?:\-([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]))?$/

请检查此内置的Ruby类Resolv :: IPv4 :: Regex中的IP验证以及此URI中的域名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM