Skip to content

Commit 4a3b300

Browse files
committed
add support for git repos
1 parent beea370 commit 4a3b300

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

commonregex.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const (
2929
ISBN10Pattern = `(?:[\d]-?){9}[\dxX]`
3030
MACAddressPattern = `(([a-fA-F0-9]{2}[:-]){5}([a-fA-F0-9]{2}))`
3131
IBANPattern = `[A-Z]{2}\d{2}[A-Z0-9]{4}\d{7}([A-Z\d]?){0,16}`
32+
GitRepoPattern = `((git|ssh|http(s)?)|(git@[\w\.]+))(:(\/\/)?)([\w\.@\:/\-~]+)(\.git)(\/)?`
3233
)
3334

3435
var (
@@ -58,6 +59,7 @@ var (
5859
MCCreditCardRegex = regexp.MustCompile(MCCreditCardPattern)
5960
MACAddressRegex = regexp.MustCompile(MACAddressPattern)
6061
IBANRegex = regexp.MustCompile(IBANPattern)
62+
GitRepoRegex = regexp.MustCompile(GitRepoPattern)
6163
)
6264

6365
func match(text string, regex *regexp.Regexp) []string {
@@ -168,3 +170,7 @@ func MACAddresses(text string) []string {
168170
func IBANs(text string) []string {
169171
return match(text, IBANRegex)
170172
}
173+
174+
func GitRepos(text string) []string {
175+
return match(text, GitRepoRegex)
176+
}

commonregex_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,3 +635,28 @@ func TestCommonRegex_IBANs(t *testing.T) {
635635
assert.NotEqual([]string{test}, parsed, "they should not be matched")
636636
}
637637
}
638+
639+
func TestCommonRegex_GitRepos(t *testing.T) {
640+
t.Parallel()
641+
assert := assert.New(t)
642+
643+
tests := []string{
644+
"https://github.com/mingrammer/commonregex.git",
645+
"git@github.com:mingrammer/commonregex.git",
646+
}
647+
648+
failingTests := []string{
649+
"https://github.com/mingrammer/commonregex",
650+
"test@github.com:mingrammer/commonregex.git",
651+
}
652+
653+
for _, test := range tests {
654+
parsed := GitRepos(test)
655+
assert.Equal([]string{test}, parsed, "they should be matched")
656+
}
657+
658+
for _, test := range failingTests {
659+
parsed := GitRepos(test)
660+
assert.NotEqual([]string{test}, parsed, "they should not be matched")
661+
}
662+
}

0 commit comments

Comments
 (0)