Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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中验证URL有什么问题?_Ruby_Regex - Fatal编程技术网

什么';这个正则表达式在Ruby中验证URL有什么问题?

什么';这个正则表达式在Ruby中验证URL有什么问题?,ruby,regex,Ruby,Regex,我正在传递一个URL数组以进行验证。函数如下所示。 当我只传递一个URL,但不传递多个URL时,它就起作用了。正则表达式似乎是正确的。我哪里做错了 def check_urls (list) regexp =/(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix list.each do |url| if not reg

我正在传递一个URL数组以进行验证。函数如下所示。 当我只传递一个URL,但不传递多个URL时,它就起作用了。正则表达式似乎是正确的。我哪里做错了

  def check_urls (list) 
    regexp =/(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix    
    list.each do |url| 
        if not regexp.match(url) 
            return false 
        end 
    end 
    return true 
  end

修正了错误。regex函数没有什么问题,只是拆分做得不对


感谢所有花时间提供帮助的人。

在将每个url与正则表达式匹配之前,请尝试检查它:

def check_urls (list) 
  regexp =/(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix        
  list.all? do |url|
    p url # see what URL is getting inspected
    regexp =~ url  # make sure it matches the regexp
  end
end

这将帮助您找到与之不匹配的URL,您可以从那里开始工作。

在将每个URL与正则表达式匹配之前,请尝试检查每个URL:

def check_urls (list) 
  regexp =/(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix        
  list.all? do |url|
    p url # see what URL is getting inspected
    regexp =~ url  # make sure it matches the regexp
  end
end

这将帮助您找到与之不匹配的URL,您可以从那里开始工作。

也许您可以尝试解析URI并在出现错误时中止

def check_urls(list = [])
  list.to_a.all? do |uri_string|
    uri = URI.parse(uri_string) rescue nil
    uri && uri.scheme == "http"
  end
end

另请参阅的文档和

也许您可以尝试解析URI并在出现错误时中止

def check_urls(list = [])
  list.to_a.all? do |uri_string|
    uri = URI.parse(uri_string) rescue nil
    uri && uri.scheme == "http"
  end
end
另请参见和签出的文档。这是一个很棒的小工具,在很多情况下都帮了我的忙。

请查看。这是一个很好的小工具,在许多情况下帮助了我。

好的,找到了问题所在。 我正试着像这样把绳子分开

用户\u输入。拆分(“\r\n”)

看起来这是错误的,这就是为什么函数对一个值而不是多个值正确工作的原因,因为数组始终包含一个字符串,即输入字符串:-(

确定,找到问题。 我正试着像这样把绳子分开

用户\u输入。拆分(“\r\n”)


看起来这是错误的,这就是为什么函数对一个值而不是多个值正确工作的原因,因为数组始终包含一个字符串,即输入字符串:-(

此方法会满足您的要求。 它也可以位于
String
类中:

def linkify text
  text.gsub!(/\b((https?:\/\/|ftps?:\/\/|mailto:|www\.)([A-Za-z0-9\-_=%&@\?\.\/]+))\b/) {
    match = $1
    tail  = $3
    case match
    when /^www/     then  "<a href=\"http://#{match}\">Link</a>"
    when /^mailto/  then  "<a href=\"#{match}\">Link</a>"
    else                  "<a href=\"#{match}\">Link</a>"
    end
  }
  text
end
def linkify文本
text.gsub!(/\b((https?:\/\/\/\ftps?:\/\/\124; mailto:| www\)([A-Za-z0-9\-\=%&@\?\/]+)\b/){
匹配=1美元
尾=3美元
案例匹配
当/^www/然后“”
当/^mailto/然后“”
否则“
结束
}
文本
结束

此方法满足您的要求。 它也可以位于
String
类中:

def linkify text
  text.gsub!(/\b((https?:\/\/|ftps?:\/\/|mailto:|www\.)([A-Za-z0-9\-_=%&@\?\.\/]+))\b/) {
    match = $1
    tail  = $3
    case match
    when /^www/     then  "<a href=\"http://#{match}\">Link</a>"
    when /^mailto/  then  "<a href=\"#{match}\">Link</a>"
    else                  "<a href=\"#{match}\">Link</a>"
    end
  }
  text
end
def linkify文本
text.gsub!(/\b((https?:\/\/\/\ftps?:\/\/\124; mailto:| www\)([A-Za-z0-9\-\=%&@\?\/]+)\b/){
匹配=1美元
尾=3美元
案例匹配
当/^www/然后“”
当/^mailto/然后“”
否则“
结束
}
文本
结束

什么样的数据在“列表”中?请发布调用函数的代码、您正在使用的样本值(例如通过irb)以及在错误情况下发生的情况(同样,irb会话也很好)。期望值是什么?如果其中一个不好,它是否会失败?返回所有成功(true)或失败(false)在列表中?请澄清用户期望值。找到问题,此函数没有问题。输入错误。我使用了拆分方法,如此用户\u input.split(“\r\n”)通过换行从文本区域分割文本。这不起作用。有什么建议吗?列表中的所有元素取决于第一个元素。metod仅在检查第一个元素后返回。“列表”中有什么类型的数据?请发布调用函数的代码、使用的示例值(例如通过irb)以及在错误情况下发生的情况(同样,irb会话也很好)。期望值是什么?如果一个会话不好,它会失败吗?返回列表中的所有成功(true)或失败(false)?请澄清用户期望值。找到问题,此函数没有问题。输入是错误的。我使用了拆分方法,就像这个用户\u input.split(“\r\n”)通过换行从文本区域拆分文本。这不起作用。有什么建议吗?列表中的所有元素都依赖于第一个元素。metod仅在检查第一个元素后返回。