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++
#include <bits/stdc++.h>
#include <regex>
using namespace std;
string isValid_GIT_Repository(string str)
{
const regex pattern(
"((http|git|ssh|http(s)|file|\\/"
"?)|(git@[\\w\\.]+))(:(\\/\\/)?)([\\w\\.@\\:/"
"\\-~]+)(\\.git)(\\/)?" );
if (str.empty()) {
return "false" ;
}
if (regex_match(str, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "http:// host.xz/path/to/repo.git/" ;
cout << isValid_GIT_Repository(str1) << endl;
string str2 = "git@git.smartbox.in:smartbox/american_locker.git" ;
cout << isValid_GIT_Repository(str2) << endl;
string str3 = "git@git.smartbox.in:smartbox/american_locker.git" ;
cout << isValid_GIT_Repository(str3) << endl;
string str4 = "git@github.com:someone/someproject.txt" ;
cout << isValid_GIT_Repository(str4) << endl;
string str5 = "https:// git.smartbox.in" ;
cout << isValid_GIT_Repository(str5) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValid_GIT_Repository(String str)
{
String regex
= "((http|git|ssh|http(s)|file|\\/?)|"
+ "(git@[\\w\\.]+))(:(\\/\\/)?)"
+ "([\\w\\.@\\:/\\-~]+)(\\.git)(\\/)?" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "http:/"
+ "/host.xz/path/to/repo.git/" ;
System.out.println(isValid_GIT_Repository(str1));
String str2
= "git@git.smartbox.in:smartbox/american_locker.git" ;
System.out.println(isValid_GIT_Repository(str2));
String str3
= "git@git.smartbox.in:smartbox/american_locker.git" ;
System.out.println(isValid_GIT_Repository(str3));
String str4
= "git@github.com:someone/someproject.txt" ;
System.out.println(isValid_GIT_Repository(str4));
String str5 = "https:/"
+ "/git.smartbox.in" ;
System.out.println(isValid_GIT_Repository(str5));
}
}
|
Python3
import re
def isValid_GIT_Repository( str ):
regex = "((http|git|ssh|http(s)|file|\/?)" \
"|(git@[\w\.]+))(:(\/\/)?)([\w\.@\:/\-~]+)(\.git)(\/)?"
p = re. compile (regex)
if ( str = = None ):
return "false"
if (re.search(p, str )):
return "true"
else :
return "false"
if __name__ = = '__main__' :
print (isValid_GIT_Repository(str1))
str2 = "git@git.smartbox.in:smartbox / american_locker.git"
print (isValid_GIT_Repository(str2))
str3 = "git@git.smartbox.in:smartbox / american_locker.git"
print (isValid_GIT_Repository(str3))
str4 = "git@github.com:someone / someproject.txt"
print (isValid_GIT_Repository(str4))
print (isValid_GIT_Repository(str5))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG {
public static bool isValid_GIT_Repository(String str)
{
var regex = new Regex(
"((http|git|ssh|http(s)|file|\\/?)|"
+ "(git@[\\w\\.]+))(:(\\/\\/)?)"
+ "([\\w\\.@\\:/\\-~]+)(\\.git)(\\/)?" );
if (str == null ) {
return false ;
}
var m = regex.Match(str);
return m.Success;
}
public static void Main(String[] args)
{
Console.WriteLine(GFG.isValid_GIT_Repository(str1));
var str2
= "git@git.smartbox.in:smartbox/american_locker.git" ;
Console.WriteLine(GFG.isValid_GIT_Repository(str2));
var str3
= "git@git.smartbox.in:smartbox/american_locker.git" ;
Console.WriteLine(GFG.isValid_GIT_Repository(str3));
var str4 = "git@github.com:someone/someproject.txt" ;
Console.WriteLine(GFG.isValid_GIT_Repository(str4));
Console.WriteLine(GFG.isValid_GIT_Repository(str5));
}
}
|
Javascript
function isValid_GIT_Repository(str) {
let regex = new RegExp(/((http|git|ssh|http(s)|file|\/?)|(git@[\w\.]+))(:(\/\/)?)([\w\.@\:/\-~]+)(\.git)(\/)?/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
console.log(isValid_GIT_Repository(str1));
let str2 = "git@git.smartbox.in:smartbox/american_locker.git" ;
console.log(isValid_GIT_Repository(str2));
let str3 = "git@git.smartbox.in:smartbox/american_locker.git" ;
console.log(isValid_GIT_Repository(str3));
let str4 = "git@github.com:someone/someproject.txt" ;
console.log(isValid_GIT_Repository(str4));
console.log(isValid_GIT_Repository(str5));
|
Output
true
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:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...