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 MICR Code using Regular Expression? MICR stands for Magnetic Ink Character Recognition. This technology provides transaction security, ensuring the correctness of bank cheques. MICR code makes cheque processing faster and safer. MICR Technology reduces cheque-related fraudulent activities. Structure of a Magnetic Ink Character Recogni
5 min read
How to validate MAC address using Regular Expression Given string str, the task is to check whether the given string is a valid MAC address or not by using Regular Expression. A valid MAC address must satisfy the following conditions: It must contain 12 hexadecimal digits.One way to represent them is to form six pairs of the characters separated with
6 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
Regular Expressions to Validate ISBN Code Given some ISBN Codes, the task is to check if they are valid or not using regular expressions. Rules for the valid codes are: It is a unique 10 or 13-digit.It may or may not contain a hyphen.It should not contain whitespaces and other special characters.It does not allow alphabet letters. Examples:
5 min read
How to validate a Username using Regular Expressions in Java Given a string str which represents a username, the task is to validate this username with the help of Regular Expressions. A username is considered valid if all the following constraints are satisfied: The username consists of 6 to 30 characters inclusive. If the username consists of less than 6 or
3 min read
How to validate pin code of India using Regular Expression Given a string of positive number ranging from 0 to 9, the task is to check whether the number is valid pin code or not by using a Regular Expression. The valid pin code of India must satisfy the following conditions. It can be only six digits.It should not start with zero.First digit of the pin cod
6 min read
How to validate image file extension using Regular Expression Given string str, the task is to check whether the given string is a valid image file extension or not by using Regular Expression. The valid image file extension must specify the following conditions: It should start with a string of at least one character.It should not have any white space.It shou
5 min read
How to validate SSN (Social Security Number) using Regular Expression Given string str, the task is to check whether the given string is valid SSN (Social Security Number) or not by using Regular Expression. The valid SSN (Social Security Number) must satisfy the following conditions: It should have 9 digits.It should be divided into 3 parts by hyphen (-).The first pa
6 min read