TheJediCowboy
TheJediCowboy

Reputation: 9232

Golang regex to parse out repo name from git url

I need a way of parsing out the repo name from a git repo url WITHOUT using a split function.

For example I want to be able to do the following

url := "[email protected]:myorg/repo.git"
repoName := parseRepoName(url)
log.Println(repoName)  //prints "repo.git"

Upvotes: 0

Views: 2098

Answers (2)

Danilo
Danilo

Reputation: 3327

Save yourself the trouble of using a regex where you don't need one and just use:

name := url[strings.LastIndex(url, "/")+1:]

Or if you are not sure that the url is a valid github url:

i := strings.LastIndex(url, "/")
if i != -1 {
    // Do some error handling here
}
name := url[i+1:]

Upvotes: 3

Sabuj Hassan
Sabuj Hassan

Reputation: 39395

I am not that much familiar with golang till days. May be you can go with replace instead of split. For example using the following pseudocode.

Make a regex .*/ and then replace it with your string.

reg.ReplaceAllString(url, "")

This will replace anything before the last / and you'll have the repo.git

Upvotes: 1

Related Questions