Validate GIT Repository using Regular Expression
Last Updated :
10 Feb, 2023
GIT stands for GLOBAL INFORMATION TRACKER.
Given some Git repositories, the task is to check if they are valid or not using regular expressions. Rules for the valid Git Repository are:
- It is an alphanumeric string containing uppercase Alphabet letters(A-Z) and digits(0-9).
- It should not contain any white spaces.
- It can contain some special symbols like "@", "/", "-", "." and ":".
- it should always start with the alphabet.
Examples:
Input: str = “http://host.xz/path/to/repo.git/”
Output: True
Input: str = "https://git.smartbox.in"
Output: False
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex= "((http|git|ssh|http(s)|file|\/?)|(git@[\w\.]+))(:(\/\/)?)([\w\.@\:/\-~]+)(\.git)(\/)?"
Where,
- \ : Represents one of this should be present and should be starting of the string.
- ? : Either one should be present or not.
- (s) : s can be attached to http or not
- \. : Represents dot should be there.
Follow the below steps to implement the idea:
- Create a regex expression for Git Repository.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the Git Repository is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the implementation of the above approach:
C++
// C++ program to validate the
// GIT Repository using Regular
// Expression
#include <bits/stdc++.h>
#include <regex>
using namespace std;
// Function to validate the
// GIT Repository
string isValid_GIT_Repository(string str)
{
// Regex to check valid
// GIT Repository
const regex pattern(
"((http|git|ssh|http(s)|file|\\/"
"?)|(git@[\\w\\.]+))(:(\\/\\/)?)([\\w\\.@\\:/"
"\\-~]+)(\\.git)(\\/)?");
// If the str
// is empty return false
if (str.empty()) {
return "false";
}
// Return true if the str
// matched the ReGex
if (regex_match(str, pattern)) {
return "true";
}
else {
return "false";
}
}
// Driver Code
int main()
{
// Test Case 1:
string str1 = "http:// host.xz/path/to/repo.git/";
cout << isValid_GIT_Repository(str1) << endl;
// Test Case 2:
string str2 = "[email protected]:smartbox/american_locker.git";
cout << isValid_GIT_Repository(str2) << endl;
// Test Case 3:
string str3 = "[email protected]:smartbox/american_locker.git";
cout << isValid_GIT_Repository(str3) << endl;
// Test Case 4:
string str4 = "[email protected]:someone/someproject.txt";
cout << isValid_GIT_Repository(str4) << endl;
// Test Case 5:
string str5 = "https:// git.smartbox.in";
cout << isValid_GIT_Repository(str5) << endl;
return 0;
}
Java
// Java program to validate the
// GIT Repository using Regular Expression
import java.util.regex.*;
class GFG {
// Function to validate the
// GIT Repository
public static boolean isValid_GIT_Repository(String str)
{
// Regex to check valid GIT Repository
String regex
= "((http|git|ssh|http(s)|file|\\/?)|"
+ "(git@[\\w\\.]+))(:(\\/\\/)?)"
+ "([\\w\\.@\\:/\\-~]+)(\\.git)(\\/)?";
// Compile the ReGex
Pattern p = Pattern.compile(regex);
// If the str is empty return false
if (str == null) {
return false;
}
// Pattern class contains matcher()
// method to find matching between
// given str using regex.
Matcher m = p.matcher(str);
// Return if the str
// matched the ReGex
return m.matches();
}
// Driver Code.
public static void main(String args[])
{
// Test Case 1:
String str1 = "http:/"
+ "/host.xz/path/to/repo.git/";
System.out.println(isValid_GIT_Repository(str1));
// Test Case 2:
String str2
= "[email protected]:smartbox/american_locker.git";
System.out.println(isValid_GIT_Repository(str2));
// Test Case 3:
String str3
= "[email protected]:smartbox/american_locker.git";
System.out.println(isValid_GIT_Repository(str3));
// Test Case 4:
String str4
= "[email protected]:someone/someproject.txt";
System.out.println(isValid_GIT_Repository(str4));
// Test Case 5:
String str5 = "https:/"
+ "/git.smartbox.in";
System.out.println(isValid_GIT_Repository(str5));
}
}
Python3
# Python3 program to validate
# GIT Repository using Regular Expression
import re
# Function to validate
# GIT Repository
def isValid_GIT_Repository(str):
# Regex to check valid GIT Repository
regex = "((http|git|ssh|http(s)|file|\/?)"\
"|(git@[\w\.]+))(:(\/\/)?)([\w\.@\:/\-~]+)(\.git)(\/)?"
# 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
if __name__ == '__main__':
# Test Case 1:
str1 = "http://host.xz / path / to / repo.git/"
print(isValid_GIT_Repository(str1))
# Test Case 2:
str2 = "[email protected]:smartbox / american_locker.git"
print(isValid_GIT_Repository(str2))
# Test Case 3:
str3 = "[email protected]:smartbox / american_locker.git"
print(isValid_GIT_Repository(str3))
# Test Case 4:
str4 = "[email protected]:someone / someproject.txt"
print(isValid_GIT_Repository(str4))
# Test Case 5:
str5 = "https://git.smartbox.in"
print(isValid_GIT_Repository(str5))
C#
// Include namespace system
using System;
using System.Text.RegularExpressions;
public class GFG {
public static bool isValid_GIT_Repository(String str)
{
// Function to validate the
// GIT Repository
var regex = new Regex(
"((http|git|ssh|http(s)|file|\\/?)|"
+ "(git@[\\w\\.]+))(:(\\/\\/)?)"
+ "([\\w\\.@\\:/\\-~]+)(\\.git)(\\/)?");
// If the LEI Code
// is empty return false
if (str == null) {
return false;
}
// Pattern class contains matcher()
// method to find matching between
// given LEI Code using regex.
var m = regex.Match(str);
// Return if the str
// matched the ReGex
return m.Success;
}
// Driver Code.
public static void Main(String[] args)
{
// Test Case 1:
var str1 = "http://host.xz/path/to/repo.git/";
Console.WriteLine(GFG.isValid_GIT_Repository(str1));
// Test Case 2:
var str2
= "[email protected]:smartbox/american_locker.git";
Console.WriteLine(GFG.isValid_GIT_Repository(str2));
// Test Case 3:
var str3
= "[email protected]:smartbox/american_locker.git";
Console.WriteLine(GFG.isValid_GIT_Repository(str3));
// Test Case 4:
var str4 = "[email protected]:someone/someproject.txt";
Console.WriteLine(GFG.isValid_GIT_Repository(str4));
// Test Case 5:
var str5 = "https:// git.smartbox.in";
Console.WriteLine(GFG.isValid_GIT_Repository(str5));
}
}
// This code is contributed by Potta Lokesh
JavaScript
// Javascript program to validate
// GIT Repository using Regular Expression
// Function to validate the
// GIT Repository
function isValid_GIT_Repository(str) {
// Regex to check valid
// GIT Repository
let regex = new RegExp(/((http|git|ssh|http(s)|file|\/?)|(git@[\w\.]+))(:(\/\/)?)([\w\.@\:/\-~]+)(\.git)(\/)?/);
// if str
// is empty return false
if (str == null) {
return "false";
}
// Return true if the str
// matched the ReGex
if (regex.test(str) == true) {
return "true";
}
else {
return "false";
}
}
// Driver Code
// Test Case 1:
let str1 = "http://host.xz/path/to/repo.git/";
console.log(isValid_GIT_Repository(str1));
// Test Case 2:
let str2 = "[email protected]:smartbox/american_locker.git";
console.log(isValid_GIT_Repository(str2));
// Test Case 3:
let str3 = "[email protected]:smartbox/american_locker.git";
console.log(isValid_GIT_Repository(str3));
// Test Case 4:
let str4 = "[email protected]:someone/someproject.txt";
console.log(isValid_GIT_Repository(str4));
// Test Case 5:
let str5 = "https://git.smartbox.in";
console.log(isValid_GIT_Repository(str5));
Outputtrue
true
true
false
false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Similar Reads
How to validate ISIN using Regular Expressions ISIN stands for International Securities Identification Number. Given string str, the task is to check whether the given string is a valid ISIN(International Securities Identification Number) or not by using Regular Expression. The valid ISIN(International Securities Identification Number) must sati
6 min read
Validating UPI IDs using Regular Expressions Given some UPI IDs, the task is to check if they are valid or not using regular expressions. Rules for the valid UPI ID: UPI ID is an alphanumeric String i.e., formed using digits(0-9), alphabets (A-Z and a-z), and other special characters.It must contain '@'.It should not contain whitespace.It may
5 min read
How to validate a domain name using Regular Expression Given string str, the task is to check whether the given string is a valid domain name or not by using Regular Expression.The valid domain name must satisfy the following conditions: The domain name should be a-z or A-Z or 0-9 and hyphen (-).The domain name should be between 1 and 63 characters long
6 min read
How to validate GST (Goods and Services Tax) number using Regular Expression Given string str, the task is to check whether the given string is a valid GST (Goods and Services Tax) number or not using Regular Expression. The valid GST (Goods and Services Tax) number must satisfy the following conditions: It should be 15 characters long.The first 2 characters should be a numb
6 min read
Regular Expressions to Validate Google Analytics Tracking Id Given some Google Analytics Tracking IDs, the task is to check if they are valid or not using regular expressions. Rules for the valid Tracking Id are: It is an alphanumeric string i.e., containing digits (0-9), alphabets (A-Z), and a Special character hyphen(-).The hyphen will come in between the g
5 min read
How to validate GUID (Globally Unique Identifier) using Regular Expression Given string str, the task is to check whether the given string is a valid GUID (Globally Unique Identifier) or not by using Regular Expression.The valid GUID (Globally Unique Identifier) must specify the following conditions: It should be a 128-bit number.It should be 36 characters (32 hexadecimal
5 min read
Validate LEI(Legal Entity Identifier) using Regular Expression Given some Legal Entity Identifier, the task is to check whether they are valid using regular expressions. Rules for the valid LEI are: LEI code length is comprised of a 20-digit alphanumeric code.The first four characters are digits. Next two places are reserved for digit Zero.Next 12 characters ar
6 min read
Regular Expressions to validate International Tracking of Imports Given some International Tracking of Imports, the task is to check if they are valid or not using regular expressions. Rules for the valid International Tracking of Imports are: It is an alphanumeric string i.e., It contains UpperCase alphabet letters (A-Z) and digits(0-9).It does not contain whites
5 min read
Extracting Repository Name from a Given GIT URL using Regular Expressions Given a string str, the task is to extract Repository Name from the given GIT URL. Examples: GIT URL can be any of the formats mentioned below: Input: str="git://github.com/book-Store/My-BookStore.git"Output: My-BookStoreExplanation: The Repo Name of the given URL is: My-BookStore Input: str="git@gi
4 min read