I am passing an array of URLs to validate. The function is below. It works when I pass just one URL, but not more than one. the regular expression seems to be correct. Where am I going wrong?
我传递了一系列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
fixed the error. nothing wrong the regex function, just the splitting was done wrong.
修正了错误。没有错误的正则表达式功能,只是分裂是错误的。
thanks to all who took time to help.
感谢所有花时间提供帮助的人。
5 个解决方案
#1
Try inspecting each url before you match it against the 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
This should help you find the URL that doesn't match it, and you can work from there.
这应该可以帮助您找到与其不匹配的URL,您可以从那里开始工作。
#2
Maybe you could just try to parse the uris and abort on error.
也许你可以尝试解析uris并在出错时中止。
def check_urls(list = [])
list.to_a.all? do |uri_string|
uri = URI.parse(uri_string) rescue nil
uri && uri.scheme == "http"
end
end
See also the docs for Enumerable#all and URI.parse
另请参阅Enumerable#all和URI.parse的文档
#3
Check out Rubular. It's a great little tool that has helped me out in numerous cases.
看看Rubular。这是一个很棒的小工具,在很多情况下帮助了我。
#4
ok, found the prob. I am trying to split the string like this
好的,找到了问题。我试图像这样拆分字符串
user_input.split("\r\n")
looks like this is wrong, that is why the function is working correctly for one value and not more than one value, because the array always contains one string, which is the input string :-(
看起来这是错误的,这就是为什么函数正确地为一个值而不是一个值工作,因为数组总是包含一个字符串,这是输入字符串:-(
#5
This method does what you are asking. It could also be inside String
class:
这种方法可以满足您的要求。它也可以在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
#1
Try inspecting each url before you match it against the 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
This should help you find the URL that doesn't match it, and you can work from there.
这应该可以帮助您找到与其不匹配的URL,您可以从那里开始工作。
#2
Maybe you could just try to parse the uris and abort on error.
也许你可以尝试解析uris并在出错时中止。
def check_urls(list = [])
list.to_a.all? do |uri_string|
uri = URI.parse(uri_string) rescue nil
uri && uri.scheme == "http"
end
end
See also the docs for Enumerable#all and URI.parse
另请参阅Enumerable#all和URI.parse的文档
#3
Check out Rubular. It's a great little tool that has helped me out in numerous cases.
看看Rubular。这是一个很棒的小工具,在很多情况下帮助了我。
#4
ok, found the prob. I am trying to split the string like this
好的,找到了问题。我试图像这样拆分字符串
user_input.split("\r\n")
looks like this is wrong, that is why the function is working correctly for one value and not more than one value, because the array always contains one string, which is the input string :-(
看起来这是错误的,这就是为什么函数正确地为一个值而不是一个值工作,因为数组总是包含一个字符串,这是输入字符串:-(
#5
This method does what you are asking. It could also be inside String
class:
这种方法可以满足您的要求。它也可以在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