Software Development

Extracting Repository Identify from a Given GIT URL utilizing Common Expressions

Extracting Repository Identify from a Given GIT URL utilizing Common Expressions
Written by admin


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Given a string str, the duty is to extract Repository Identify from the given  GIT URL.

Examples:

GIT URL will be any of the codecs talked about under:

Enter: str=”git://github.com/book-Retailer/My-BookStore.git”
Output: My-BookStore
Clarification: The Repo Identify of the given URL is: My-BookStore

Enter: str=”git@github.com:book-Retailer/My-BookStore.git”
Output: My-BookStore
Clarification: The Repo Identify of the given URL is: My-BookStore

Enter: str=”https://github.com/book-Retailer/My-BookStore.git”
Output: My-BookStore
Clarification: The Repo Identify of the given URL is: My-BookStore

Strategy: The issue will be solved primarily based on the next concept:

Create a regex sample to validate the Repo Identify as written: regex = “([^/]+).git$“

Comply with the under steps to implement the concept:

  • Create a regex expression to extract aRepo Identify  from the string.
  • Use Sample class to compile the regex shaped.
  • Use the matcher perform to seek out.

Under is the code implementation of the above-discussed method:

Java

// Java code for the above method
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Sample;

public class GFG {

    // Driver Code
    public static void fundamental(String[] args)
    {

        // String int the type of GIT URL
        String str
            = "git:/"
              + "/github.com/book-Retailer/My-BookStore.git";
        System.out.println(" Given GIT URL is:n" + str);
        System.out.println(
            "Repository Identify of above given GIT URL is:");
        extractRepositoryName(str);
    }

    // Perform to extract Repo Identify
    // from a given string
    static void extractRepositoryName(String str)
    {

        // You possibly can Add n variety of GIT URL
        // codecs within the under given
        // String Array.
        String strPattern[] = { "([^/]+).git$" };
        for (int i = 0; i < strPattern.size; i++) {
            Sample sample
                = Sample.compile(strPattern[i]);
            Matcher matcher = sample.matcher(str);
            whereas (matcher.discover()) {
                System.out.println(matcher.group());
            }
        }
    }
}
Output

 Given GIT URL is:
git://github.com/book-Retailer/My-BookStore.git
Repository Identify of above given GIT URL is:
My-BookStore.git

Associated Articles:

About the author

admin

Leave a Comment