Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails URL验证(regex)_Ruby On Rails_Regex_Validation_Dns_Format - Fatal编程技术网

Ruby on rails RubyonRails URL验证(regex)

Ruby on rails RubyonRails URL验证(regex),ruby-on-rails,regex,validation,dns,format,Ruby On Rails,Regex,Validation,Dns,Format,我正在尝试使用正则表达式验证Rails模型中URL的格式。我已经在Rubular中测试了正则表达式,它与URL匹配 当我在Rails应用程序中测试它时,你知道为什么它没有通过验证吗(它说“名称无效”) 代码: 您的输入()没有子域,但正则表达式正在检查子域 domain_regex = /^((http|https):\/\/)[a-z0-9]*(\.?[a-z0-9]+)\.[a-z]{2,5}(:[0-9]{1,5})?(\/.)?$/ix 更新 你也不需要这个?在((http | htt

我正在尝试使用正则表达式验证Rails模型中URL的格式。我已经在Rubular中测试了正则表达式,它与URL匹配

当我在Rails应用程序中测试它时,你知道为什么它没有通过验证吗(它说“名称无效”)

代码:

您的输入()没有子域,但正则表达式正在检查子域

domain_regex = /^((http|https):\/\/)[a-z0-9]*(\.?[a-z0-9]+)\.[a-z]{2,5}(:[0-9]{1,5})?(\/.)?$/ix
更新

你也不需要这个?在((http | https):\/\/)之后,除非协议有时缺失。我也逃脱了。因为这将匹配任何字符。我不确定上面的分组是为了什么,但这里有一个更好的版本,它支持破折号和分段分组

domain_regex = /^((http|https):\/\/) 
(([a-z0-9-\.]*)\.)?                  
([a-z0-9-]+)\.                        
([a-z]{2,5})
(:[0-9]{1,5})?
(\/)?$/ix

这里不需要使用regexp。Ruby有一种更可靠的方法:

# Use the URI module distributed with Ruby:

require 'uri'

unless (url =~ URI::regexp).nil?
    # Correct URL
end
(这个答案来自:)

(我喜欢Thomas Hupkens的答案,但对于其他观看的人,我建议可以寻址)

不建议使用正则表达式验证URL

使用Ruby的URI库或类似的替代品,这两者都使得URL验证变得微不足道。与URI不同,Addressable还可以处理国际字符和TLD

用法示例:

require 'addressable/uri'

Addressable::URI.parse("кц.рф") # Works

uri = Addressable::URI.parse("http://example.com/path/to/resource/")
uri.scheme
#=> "http"
uri.host
#=> "example.com"
uri.path
#=> "/path/to/resource/"
您可以构建自定义验证,如:

class Example
  include ActiveModel::Validations

  ##
  # Validates a URL
  #
  # If the URI library can parse the value, and the scheme is valid
  # then we assume the url is valid
  #
  class UrlValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, value)
      begin
        uri = Addressable::URI.parse(value)

        if !["http","https","ftp"].include?(uri.scheme)
          raise Addressable::URI::InvalidURIError
        end
      rescue Addressable::URI::InvalidURIError
        record.errors[attribute] << "Invalid URL"
      end
    end
  end

  validates :field, :url => true
end
类示例
包括ActiveModel::验证
##
#验证URL
#
#如果URI库可以解析该值,并且该方案有效
#然后我们假设url是有效的
#
类UrlValidator
试试这个。 这对我有用

/(ftp | http | https):/(\w+:{0,1}\w*@)(\S+:[0-9]+)(/([\w#!:?+=&%@!-/])?/

这将包括一个国际主机处理,以及类似于
abc.com。它
部分是可选的

match '/:site', to: 'controller#action' , constraints: { site: /[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}(.[a-zA-Z]{2,63})?/}, via: :get, :format => false

谢谢这修复了错误,但现在像“abcd”这样的条目是有效的。你知道怎么解决吗?更新应该可以。我还删除了[-.]并将其替换为\。这不处理国际域名,国际域名可以用ASCII表示,如:www.xn--b1akcweg3a.xn--p1ai。是的,这会在您的域中为您提供双破折号,这是合法的,以及超过3个字符的顶级域(最右边的组件)。@cordsen:如果我想在
Ruby
中为
URL
编写一个正则表达式,其中包括任何
非ASCII
字符或中文字符,该怎么办?例如,
http://www.詹姆斯.com/
您能告诉我如何解决这个问题吗?在查看了addressable之后,我认为它轻而易举地赢了,感谢addressable+1,但不要假设它会引发任何异常,因为它不会。Addressable::URI.parse将在尝试尽最大努力找出URI时以静默方式失败。例如,假设您想验证不正确的URI,例如:。Addressable将调用scheme http和域http,因为它将冒号视为端口分隔符。不会出现任何错误。请注意,问题实际上是要检查URL而不是域名,域名是trentscott.com.Edited and fixed。希望谷歌能重新编制索引。
“https://foo;bar.com“=~URI::regexp
生成成功匹配。所以这不是很有用。
match '/:site', to: 'controller#action' , constraints: { site: /[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}(.[a-zA-Z]{2,63})?/}, via: :get, :format => false