由于使用网址为大小N .The任务的str是要检查如果给定的URL是否有效。
例子 :
Input : str = “https://www.geeksforgeeks.org/”
Output : Yes
Explanation :
The above URL is a valid URL.
Input : str = “https:// www.geeksforgeeks.org/”
Output : No
Explanation :
Note that there is a space after https://, hence the URL is invalid.
方法 :
上一篇文章讨论了使用Java.net.url 类来验证 URL 的方法。
这里的想法是使用正则表达式来验证 URL。
- 获取网址。
- 创建一个正则表达式来检查有效的 URL,如下所述:
regex = “((http|https)://)(www.)?”
+ “[a-zA-Z0-9@:%._\\+~#?&//=]{2,256}\\.[a-z]”
+ “{2,6}\\b([-a-zA-Z0-9@:%._\\+~#?&//=]*)”
- The URL must start with either http or https and
- then followed by :// and
- then it must contain www. and
- then followed by subdomain of length (2, 256) and
- last part contains top level domain like .com, .org etc.
- 将给定的 URL 与正则表达式匹配。在Java,这可以通过使用 Pattern.matcher() 来完成。
- 如果 URL 与给定的正则表达式匹配,则返回 true,否则返回 false。
下面是上述方法的实现:
C++
// C++ program to validate URL
// using Regular Expression
#include
#include
using namespace std;
// Function to validate URL
// using regular expression
bool isValidURL(string url)
{
// Regex to check valid URL
const regex pattern("((http|https)://)(www.)?[a-zA-Z0-9@:%._\\+~#?&//=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%._\\+~#?&//=]*)");
// If the URL
// is empty return false
if (url.empty())
{
return false;
}
// Return true if the URL
// matched the ReGex
if(regex_match(url, pattern))
{
return true;
}
else
{
return false;
}
}
// Driver Code
int main()
{
string url = "https://www.geeksforgeeks.org";
if (isValidURL(url))
{
cout << "YES";
}
else
{
cout << "NO";
}
return 0;
}
// This code is contributed by yuvraj_chandra
Java
// Java program to check URL is valid or not
// using Regular Expression
import java.util.regex.*;
class GFG {
// Function to validate URL
// using regular expression
public static boolean
isValidURL(String url)
{
// Regex to check valid URL
String regex = "((http|https)://)(www.)?"
+ "[a-zA-Z0-9@:%._\\+~#?&//=]"
+ "{2,256}\\.[a-z]"
+ "{2,6}\\b([-a-zA-Z0-9@:%"
+ "._\\+~#?&//=]*)";
// Compile the ReGex
Pattern p = Pattern.compile(regex);
// If the string is empty
// return false
if (url == null) {
return false;
}
// Find match between given string
// and regular expression
// using Pattern.matcher()
Matcher m = p.matcher(url);
// Return if the string
// matched the ReGex
return m.matches();
}
// Driver code
public static void main(String args[])
{
String url
= "https://www.geeksforgeeks.org";
if (isValidURL(url) == true) {
System.out.println("Yes");
}
else
System.out.println("NO");
}
}
Python3
# Python3 program to check
# URL is valid or not
# using regular expression
import re
# Function to validate URL
# using regular expression
def isValidURL(str):
# Regex to check valid URL
regex = ("((http|https)://)(www.)?" +
"[a-zA-Z0-9@:%._\\+~#?&//=]" +
"{2,256}\\.[a-z]" +
"{2,6}\\b([-a-zA-Z0-9@:%" +
"._\\+~#?&//=]*)")
# Compile the ReGex
p = re.compile(regex)
# If the string is empty
# return false
if (str == None):
return False
# Return if the string
# matched the ReGex
if(re.search(p, str)):
return True
else:
return False
# Driver code
# Test Case 1:
url = "https://www.geeksforgeeks.org"
if(isValidURL(url) == True):
print("Yes")
else:
print("No")
# This code is contributed by avanitrachhadiya2155
输出:
Yes
时间复杂度: O (N)
辅助空间: O (1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live