Open In App

Validate GIT Repository using Regular Expression

Last Updated : 10 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 = "git@git.smartbox.in:smartbox/american_locker.git";
    cout << isValid_GIT_Repository(str2) << endl;
 
    // Test Case 3:
    string str3 = "git@git.smartbox.in:smartbox/american_locker.git";
    cout << isValid_GIT_Repository(str3) << endl;
 
    // Test Case 4:
    string str4 = "git@github.com: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
            = "git@git.smartbox.in:smartbox/american_locker.git";
        System.out.println(isValid_GIT_Repository(str2));
 
        // Test Case 3:
        String str3
            = "git@git.smartbox.in:smartbox/american_locker.git";
        System.out.println(isValid_GIT_Repository(str3));
 
        // Test Case 4:
        String str4
            = "git@github.com: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 = "git@git.smartbox.in:smartbox / american_locker.git"
    print(isValid_GIT_Repository(str2))
     
    # Test Case 3:
    str3 = "git@git.smartbox.in:smartbox / american_locker.git"
    print(isValid_GIT_Repository(str3))
     
    # Test Case 4:
    str4 = "git@github.com: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
      = "git@git.smartbox.in:smartbox/american_locker.git";
    Console.WriteLine(GFG.isValid_GIT_Repository(str2));
 
    // Test Case 3:
    var str3
      = "git@git.smartbox.in:smartbox/american_locker.git";
    Console.WriteLine(GFG.isValid_GIT_Repository(str3));
 
    // Test Case 4:
    var str4 = "git@github.com: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:
console.log(isValid_GIT_Repository(str1));
 
// Test Case 2:
let str2 = "git@git.smartbox.in:smartbox/american_locker.git";
console.log(isValid_GIT_Repository(str2));
 
// Test Case 3:
let str3 = "git@git.smartbox.in:smartbox/american_locker.git";
console.log(isValid_GIT_Repository(str3));
 
// Test Case 4:
let str4 = "git@github.com:someone/someproject.txt";
console.log(isValid_GIT_Repository(str4));
 
// Test Case 5:
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
Previous
Next
Share your thoughts in the comments

Similar Reads