Reputation: 9648
I want to use regex to validate git repository url. I found a few answers on stackoverflow but none of them passes my tests.
The debug is here: http://regexr.com/39qia
How can I make it passes the last four cases?
[email protected]:group-name/project-name.git
[email protected]:group-name/project-name.git
http://host.xy/agroup-name/project-name.git
http://ho-st.xy/agroup-name/project-name.git
Upvotes: 1
Views: 1454
Reputation: 3003
Ok, the following expression matches all of your current test-text and does not match any of your false positives provided before:
((((git|user)@[\w.-]+)|(git|ssh|http(s)?|file))(:(\/){0,3}))?([\w.@\:/~\-]+)(\.git)(\/)?
See also, regex.
Caveat: Be aware, that currently input is matched with '~' and '-' appearing in places where they shouldn't.
Upvotes: 0
Reputation: 26301
You can try this one:
(?'protocol'git@|https?:\/\/)(?'domain'[a-zA-Z0-9\.\-_]+)(\/|:)(?'group'[a-zA-Z0-9\-]+)\/(?'project'[a-zA-Z0-9\-]+)\.git
You can then extract the needed information from the matched groups.
You can test this regex on: Regex101
Upvotes: 0
Reputation: 71578
I can't be certain since I'm not familiar with git link syntaxes, but the following regex will additionally match the 4 next values:
((git|ssh|http(s)?)|(git@[\w.-]+))(:(//)?)([\w.@\:/~-]+)(\.git)(/)?
^ ^^ ^
I have indicated the changed parts; namely:
-
to the part after @
because ho-st
was not passing otherwise.-
to the end of the character class because otherwise /-~
would mean the character range /
to ~
which matches a lot of characters.There are a lot of things that could be simplified from the above, but since I don't know your exact goals, I'm leaving the regex as close as possible to the one you have.
Upvotes: 1