-
Notifications
You must be signed in to change notification settings - Fork 4
/
regex.go
85 lines (77 loc) · 2.35 KB
/
regex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"regexp"
"strings"
)
var (
/*
Regex`s for checking input
*/
ipv6Regex = `^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$`
ipv4Regex = `^(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4})`
domainRegex = `^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z]$`
portRegex = "^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([1-9][0-9]{3})|([1-9][0-9]{2})|([1-9][0-9])|([1-9]))$"
)
//IsIPv4 is it valid IPv4 address
func IsIPv4(host string) bool {
match, err := regexp.MatchString(ipv4Regex, host)
if err != nil {
fmt.Println(err)
return false
}
return match
}
//IsIPv6 is it valid IPv6 address
func IsIPv6(host string) bool {
match, err := regexp.MatchString(ipv6Regex, host)
if err != nil {
fmt.Println(err)
return false
}
return match
}
//IsPort is it valid port number
func IsPort(port string) bool {
match, err := regexp.MatchString(portRegex, port)
if err != nil {
fmt.Println(err)
return false
}
return match
}
//IsDomain check is valid host
func IsDomain(host string) bool {
match, err := regexp.MatchString(domainRegex, host)
if err != nil {
fmt.Println(err)
return false
}
return match
}
func DomainContains(text string, search string, mode string) bool {
words := strings.Fields(text)
for _, arg := range words {
if StartWithHost(arg, search, mode) {
return true
}
}
return false
}
func StartWithHost(text string, search string, mode string) bool {
if mode == "ip" {
match, err := regexp.MatchString("^"+search, text)
if err != nil {
fmt.Println(err)
return false
}
return match
} else {
match, err := regexp.MatchString("^(\\[)?"+search, text)
if err != nil {
fmt.Println(err)
return false
}
return match
}
}