Skip to content

Commit

Permalink
Fix IsValidAddress() to support leading digit (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
benbjohnson authored Dec 21, 2023
1 parent 384b590 commit 3aba1da
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion stringutil/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

var (
// HostnameRegex is the regex for valid hostnames.
HostnameRegex = regexp.MustCompile(`^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$`)
HostnameRegex = regexp.MustCompile(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$`)
// IPV4Regex is the regex for valid IPv4 addresses.
IPV4Regex = regexp.MustCompile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`)
)
Expand Down
16 changes: 16 additions & 0 deletions stringutil/regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ func TestIsValidAddressErr(t *testing.T) {
value: "service_1.dc1.ru:6060",
positive: false,
},
{
value: "123service:6060",
positive: true,
},
{
value: "123service.dc1.ru:6060",
positive: true,
},
{
value: "1:6060",
positive: true,
},
{
value: "1.dc1.ru:6060",
positive: true,
},
} {
tc := tc
t.Run(tc.value, func(t *testing.T) {
Expand Down

0 comments on commit 3aba1da

Please sign in to comment.