Anonymous
Anonymous

Reputation: 9648

Regex for git's repository

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

Answers (3)

participant
participant

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

Matias Cicero
Matias Cicero

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

Jerry
Jerry

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:

  1. Added - to the part after @ because ho-st was not passing otherwise.
  2. Moved - to the end of the character class because otherwise /-~ would mean the character range / to ~ which matches a lot of characters.
  3. Escaped the final dot (thanks @MatiCicero)

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

Related Questions