Regex ninjas help

to any regex ninja out there.
I’m trying to make a regex to grab only the repository name from different git urls

example inputs

https://github.com/dimitre/ofxMicroUI
git@github.com:astellato/ofxSyphon.git
http://github.com/dimitre/ofxTools

example outputs

ofxMicroUI
ofxSyphon
ofxTools

the one I’m working now is simple, but there is something missing there, the result is outputting ofxSyphon.git instead of ofxSyphon

	std::regex findRepository("([^/]+)(\.git)?$");

Thank you!

Hey @dimitre,
Here’s one that I just wrote, I hope it fits your needs.

^(https?:\/\/|git@)([^:/]+)[:/]{1}(.+)/([^./]+)?(?:.git)?$

I tested it on RegexR with some extra git hosts just in case.
If you don’t know that site, I recommend it, it explains regexes very well.
I didn’t test it in c++, sometimes there can be slight differences.

Or, if you only want the 4th group :

^(?:https?:\/\/|git@)(?:[^:/]+)[:/]{1}(?:.+)/([^./]+)(?:.git)?$
1 Like

Thank you! works great