Best way to check if a URL is valid
You can use a native Filter Validator
filter_var($url, FILTER_VALIDATE_URL);
Validates value as URL (according to » http://www.faqs.org/rfcs/rfc2396), optionally with required components. Beware a valid URL may not specify the HTTP protocol http:// so further validation may be required to determine the URL uses an expected protocol, e.g. ssh:// or mailto:. Note that the function will only find ASCII URLs to be valid; internationalized domain names (containing non-ASCII characters) will fail.
Example:
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
die('Not a valid URL');
}
Check if a JavaScript string is a URL
A related question with an answer
Or this Regexp from Devshed:
function validURL(str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
return !!pattern.test(str);
}
How to check url whether url is valid in lua?
The error is fairly clear: \/
is an invalid escape. You don't need to escape /
, as it's not a special character in Lua patterns (check the list of "magic" characters) and removing the escape should work: string.match('https://stackoverflow.com/', '[a-z]*://[^ >,;]*')
.
How to check whether a string is a valid HTTP URL?
Try this to validate HTTP URLs (uriName
is the URI you want to test):
Uri uriResult;
bool result = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult)
&& uriResult.Scheme == Uri.UriSchemeHttp;
Or, if you want to accept both HTTP and HTTPS URLs as valid (per J0e3gan's comment):
Uri uriResult;
bool result = Uri.TryCreate(uriName, UriKind.Absolute, out uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
How to validate a url in Python? (Malformed or not)
django url validation regex (source):
import re
regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
print(re.match(regex, "http://www.example.com") is not None) # True
print(re.match(regex, "example.com") is not None) # False
How to check if a URL is valid
Notice:
As pointed by @CGuess, there's a bug with this issue and it's been documented for over 9 years now that validation is not the purpose of this regular expression (see https://bugs.ruby-lang.org/issues/6520).
Use the URI
module distributed with Ruby:
require 'uri'
if url =~ URI::regexp
# Correct URL
end
Like Alexander Günther said in the comments, it checks if a string contains a URL.
To check if the string is a URL, use:
url =~ /\A#{URI::regexp}\z/
If you only want to check for web URLs (http
or https
), use this:
url =~ /\A#{URI::regexp(['http', 'https'])}\z/
Related Topics
How to Count Duplicate Elements in a Ruby Array
Incompatible Character Encodings: Ascii-8Bit and Utf-8
Converting a Nested Hash into a Flat Hash
How to Implement Enums in Ruby
Nokogiri/Xpath Namespace Query
Usage of Attr_Accessor in Rails
Rails 5: Activerecord or Query
Why Does String Interpolation Work in Ruby When There Are No Curly Braces
Using Sinatra For Larger Projects Via Multiple Files
Ruby Function to Remove All White Spaces
Save Image from Url by Paperclip
Rails Has_Many :Through Find by Extra Attributes in Join Model
Ruby: Kind_Of? Vs. Instance_Of? Vs. Is_A
What Does To_Proc Method Mean in Ruby