You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.1 KiB

package regx
import (
"net"
"net/url"
"regexp"
"strconv"
"strings"
)
func (rr *Regx) IsIpRegx(address string) bool {
ipReg := `^((0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])\.){3}(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])$`
r, _ := regexp.Compile(ipReg)
match := r.MatchString(address)
if match {
return true
}
return false
}
// IsIp check if the string is a ip address.
func (rr *Regx) IsIp(ipstr string) bool {
ip := net.ParseIP(ipstr)
return ip != nil
}
// IsIpV4 check if the string is a ipv4 address.
func (rr *Regx) IsIpV4(ipstr string) bool {
ip := net.ParseIP(ipstr)
if ip == nil {
return false
}
return strings.Contains(ipstr, ".")
}
// IsIpV6 check if the string is a ipv6 address.
func (rr *Regx) IsIpV6(ipstr string) bool {
ip := net.ParseIP(ipstr)
if ip == nil {
return false
}
return strings.Contains(ipstr, ":")
}
// IsPort check if the string is a valid net port.
func (rr *Regx) IsPort(str string) bool {
if i, err := strconv.ParseInt(str, 10, 64); err == nil && i > 0 && i < 65536 {
return true
}
return false
}
// IsUrl check if the string is url.
func (rr *Regx) IsUrl(str string) bool {
var isUrlRegexMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`)
if str == "" || len(str) >= 2083 || len(str) <= 3 || strings.HasPrefix(str, ".") {
return false
}
u, err := url.Parse(str)
if err != nil {
return false
}
if strings.HasPrefix(u.Host, ".") {
return false
}
if u.Host == "" && (u.Path != "" && !strings.Contains(u.Path, ".")) {
return false
}
return isUrlRegexMatcher.MatchString(str)
}
// IsDns check if the string is dns.
func (rr *Regx) IsDns(dns string) bool {
var isDnsRegexMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`)
return isDnsRegexMatcher.MatchString(dns)
}