gregx包名修改为gregex

This commit is contained in:
John
2018-07-11 17:06:47 +08:00
parent 261ce9fcc9
commit 9f75d6c764
15 changed files with 73 additions and 52 deletions

View File

@ -11,7 +11,7 @@ import (
"github.com/clbanning/mxj"
"encoding/xml"
"io"
"gitee.com/johng/gf/g/util/gregx"
"gitee.com/johng/gf/g/util/gregex"
"github.com/axgle/mahonia"
"errors"
"fmt"
@ -57,7 +57,7 @@ func prepare(xmlbyte []byte) error {
return reader.NewDecoder().NewReader(input), nil
}
matchStr, err := gregx.MatchString(patten, string(xmlbyte))
matchStr, err := gregex.MatchString(patten, string(xmlbyte))
if err != nil {
return err
}

4
g/g.go
View File

@ -19,7 +19,7 @@ import (
"gitee.com/johng/gf/g/net/ghttp"
"gitee.com/johng/gf/g/net/gtcp"
"gitee.com/johng/gf/g/net/gudp"
"gitee.com/johng/gf/g/util/gregx"
"gitee.com/johng/gf/g/util/gregex"
)
const (
@ -140,7 +140,7 @@ func Redis(name...string) *gredis.Redis {
if m := config.GetMap("redis"); m != nil {
// host:port[,db[,pass]]
if v, ok := m[group]; ok {
array, err := gregx.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)`, gconv.String(v))
array, err := gregex.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)`, gconv.String(v))
if err == nil {
return gredis.New(gredis.Config{
Host : array[1],

View File

@ -12,7 +12,7 @@ import (
"gitee.com/johng/gf/g/util/gconv"
"gitee.com/johng/gf/g/encoding/gjson"
"gitee.com/johng/gf/g/container/gtype"
"gitee.com/johng/gf/g/util/gregx"
"gitee.com/johng/gf/g/util/gregex"
"gitee.com/johng/gf/g/os/gtime"
)
@ -320,7 +320,7 @@ func (r *Request) IsExited() bool {
func (r *Request) GetHost() string {
host := r.parsedHost.Val()
if len(host) == 0 {
array, _ := gregx.MatchString(`(.+):(\d+)`, r.Host)
array, _ := gregex.MatchString(`(.+):(\d+)`, r.Host)
if len(array) > 1 {
host = array[1]
} else {
@ -335,7 +335,7 @@ func (r *Request) GetHost() string {
func (r *Request) GetClientIp() string {
ip := r.clientIp.Val()
if len(ip) == 0 {
array, _ := gregx.MatchString(`(.+):(\d+)`, r.RemoteAddr)
array, _ := gregex.MatchString(`(.+):(\d+)`, r.RemoteAddr)
if len(array) > 1 {
ip = array[1]
} else {

View File

@ -11,7 +11,7 @@ import (
"errors"
"strings"
"container/list"
"gitee.com/johng/gf/g/util/gregx"
"gitee.com/johng/gf/g/util/gregex"
)
// hook缓存项根据URL.Path进行缓存因此对象中带有缓存参数
@ -164,11 +164,11 @@ func (s *Server) searchHookHandler(r *Request, hook string) []*hookCacheItem {
item := e.Value.(*HandlerItem)
if strings.EqualFold(item.router.Method, gDEFAULT_METHOD) || strings.EqualFold(item.router.Method, r.Method) {
regrule, names := s.patternToRegRule(item.router.Uri)
if gregx.IsMatchString(regrule, r.URL.Path) {
if gregex.IsMatchString(regrule, r.URL.Path) {
hookItem := &hookCacheItem {item.faddr, nil}
// 如果需要query匹配那么需要重新解析URL
if len(names) > 0 {
if match, err := gregx.MatchString(regrule, r.URL.Path); err == nil {
if match, err := gregex.MatchString(regrule, r.URL.Path); err == nil {
array := strings.Split(names, ",")
if len(match) > len(array) {
hookItem.values = make(map[string][]string)

View File

@ -11,7 +11,7 @@ import (
"errors"
"strings"
"container/list"
"gitee.com/johng/gf/g/util/gregx"
"gitee.com/johng/gf/g/util/gregex"
)
// handler缓存项根据URL.Path进行缓存因此对象中带有缓存参数
@ -52,11 +52,11 @@ func (s *Server)parsePattern(pattern string) (domain, method, uri string, err er
uri = pattern
domain = gDEFAULT_DOMAIN
method = gDEFAULT_METHOD
if array, err := gregx.MatchString(`([a-zA-Z]+):(.+)`, pattern); len(array) > 1 && err == nil {
if array, err := gregex.MatchString(`([a-zA-Z]+):(.+)`, pattern); len(array) > 1 && err == nil {
method = array[1]
uri = array[2]
}
if array, err := gregx.MatchString(`(.+)@([\w\.\-]+)`, uri); len(array) > 1 && err == nil {
if array, err := gregex.MatchString(`(.+)@([\w\.\-]+)`, uri); len(array) > 1 && err == nil {
uri = array[1]
domain = array[2]
}
@ -229,11 +229,11 @@ func (s *Server) searchHandlerDynamic(r *Request) *handlerCacheItem {
item := e.Value.(*HandlerItem)
if strings.EqualFold(item.router.Method, gDEFAULT_METHOD) || strings.EqualFold(item.router.Method, r.Method) {
rule, names := s.patternToRegRule(item.router.Uri)
if gregx.IsMatchString(rule, r.URL.Path) {
if gregex.IsMatchString(rule, r.URL.Path) {
handlerItem := &handlerCacheItem{item, nil}
// 如果需要query匹配那么需要重新解析URL
if len(names) > 0 {
if match, err := gregx.MatchString(rule, r.URL.Path); err == nil {
if match, err := gregex.MatchString(rule, r.URL.Path); err == nil {
array := strings.Split(names, ",")
if len(match) > len(array) {
handlerItem.values = make(map[string][]string)

View File

@ -14,12 +14,12 @@ import (
"strings"
"regexp"
"fmt"
"gitee.com/johng/gf/g/util/gregx"
"gitee.com/johng/gf/g/util/gregex"
)
// 判断所给地址是否是一个IPv4地址
func Validate(ip string) bool {
return gregx.IsMatchString(`^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$`, ip)
return gregex.IsMatchString(`^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$`, ip)
}
// ip字符串转为整形

View File

@ -7,10 +7,10 @@
// IPv6
package gipv6
import "gitee.com/johng/gf/g/util/gregx"
import "gitee.com/johng/gf/g/util/gregex"
// 判断所给地址是否是一个IPv6地址
func Validate(ip string) bool {
return gregx.IsMatchString(`^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$|^:((:[\da-fA-F]{1,4}){1,6}|:)$|^[\da-fA-F]{1,4}:((:[\da-fA-F]{1,4}){1,5}|:)$|^([\da-fA-F]{1,4}:){2}((:[\da-fA-F]{1,4}){1,4}|:)$|^([\da-fA-F]{1,4}:){3}((:[\da-fA-F]{1,4}){1,3}|:)$|^([\da-fA-F]{1,4}:){4}((:[\da-fA-F]{1,4}){1,2}|:)$|^([\da-fA-F]{1,4}:){5}:([\da-fA-F]{1,4})?$|^([\da-fA-F]{1,4}:){6}:$`, ip)
return gregex.IsMatchString(`^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$|^:((:[\da-fA-F]{1,4}){1,6}|:)$|^[\da-fA-F]{1,4}:((:[\da-fA-F]{1,4}){1,5}|:)$|^([\da-fA-F]{1,4}:){2}((:[\da-fA-F]{1,4}){1,4}|:)$|^([\da-fA-F]{1,4}:){3}((:[\da-fA-F]{1,4}){1,3}|:)$|^([\da-fA-F]{1,4}:){4}((:[\da-fA-F]{1,4}){1,2}|:)$|^([\da-fA-F]{1,4}:){5}:([\da-fA-F]{1,4})?$|^([\da-fA-F]{1,4}:){6}:$`, ip)
}

View File

@ -21,7 +21,7 @@ import (
"os/user"
"runtime"
"path/filepath"
"gitee.com/johng/gf/g/util/gregx"
"gitee.com/johng/gf/g/util/gregex"
"gitee.com/johng/gf/g/container/gtype"
)
@ -480,7 +480,7 @@ func MainPkgPath() string {
if strings.EqualFold("<autogenerated>", file) {
// 如果是通过init包方法进入那么无法得到准确的文件路径
f = ""
} else if !gregx.IsMatchString("^" + GoRootOfBuild(), file) {
} else if !gregex.IsMatchString("^" + GoRootOfBuild(), file) {
// 不包含go源码路径
f = file
}

View File

@ -16,7 +16,7 @@ import (
"runtime"
"strconv"
"gitee.com/johng/gf/g/os/gfile"
"gitee.com/johng/gf/g/util/gregx"
"gitee.com/johng/gf/g/util/gregex"
"gitee.com/johng/gf/g/os/gfilepool"
)
@ -146,7 +146,7 @@ func (l *Logger) backtrace() string {
for i := 1; i < 10000; i++ {
if _, cfile, cline, ok := runtime.Caller(i + l.btSkip.Val()); ok {
// 不打印出go源码路径
if !gregx.IsMatchString("^" + gfile.GoRootOfBuild(), cfile) {
if !gregex.IsMatchString("^" + gfile.GoRootOfBuild(), cfile) {
backtrace += strconv.Itoa(index) + ". " + cfile + ":" + strconv.Itoa(cline) + ln
index++
}

View File

@ -81,8 +81,8 @@ func Datetime() string {
return time.Now().Format("2006-01-02 15:04:05")
}
// 字符串转换为时间对象,可以指定字符串时间格式format格式形如Y-m-d H:i:s。
// 不传递自定义格式时,默认支持的标准时间格式:
// 字符串转换为时间对象,支持的标准时间格式:
// "2017-12-14 04:51:34 +0805 LMT",
// "2006-01-02T15:04:05Z07:00",
// "2014-01-17T01:19:15+08:00",
// "2018-02-09T20:46:17.897Z",
@ -90,11 +90,7 @@ func Datetime() string {
// "2018-02-09T20:46:17Z",
// "2018-02-09 20:46:17",
// "2018-02-09",
func StrToTime(str string, format...string) (time.Time, error) {
// 优先使用用户输入日期格式进行转换
if len(format) > 0 {
return StrToTimeLayout(str, formatToStdLayout(format[0]))
}
func StrToTime(str string) (time.Time, error) {
var result time.Time
var local = time.Local
if match := timeRegex.FindStringSubmatch(str); len(match) > 0 {
@ -174,7 +170,11 @@ func StrToTime(str string, format...string) (time.Time, error) {
return result, errors.New("unsupported time format")
}
// 通过标准库layout模板解析字符串
// 字符串转换为时间对象指定字符串时间格式format格式形如Y-m-d H:i:s
func StrToTimeFormat(str string, format string) (time.Time, error) {
return StrToTimeLayout(str, formatToStdLayout(format))
}
// 字符串转换为时间对象通过标准库layout格式进行解析layout格式形如2006-01-02 15:04:05
func StrToTimeLayout(str string, layout string) (time.Time, error) {
if t, err := time.ParseInLocation(layout, str, time.Local); err == nil {
return t, nil

View File

@ -37,8 +37,28 @@ func NewFromTime (t time.Time) *Time {
}
// 从字符串转换为时间对象,复杂的时间字符串需要给定格式
func NewFromStr (str string, format...string) *Time {
if t, err := StrToTime(str, format...); err == nil {
func NewFromStr (str string) *Time {
if t, err := StrToTime(str); err == nil {
return &Time{
t,
}
}
return nil
}
// 从字符串转换为时间对象指定字符串时间格式format格式形如Y-m-d H:i:s
func NewFromStrFormat (str string, format string) *Time {
if t, err := StrToTimeFormat(str, format); err == nil {
return &Time{
t,
}
}
return nil
}
// 从字符串转换为时间对象通过标准库layout格式进行解析layout格式形如2006-01-02 15:04:05
func NewFromStrLayout (str string, layout string) *Time {
if t, err := StrToTimeLayout(str, layout); err == nil {
return &Time{
t,
}

View File

@ -5,7 +5,7 @@
// You can obtain one at https://gitee.com/johng/gf.
// 正则表达式.
package gregx
package gregex
import (
"regexp"

View File

@ -6,7 +6,7 @@
// go test *.go -bench=".*"
package gregx
package gregex
import (
"testing"

View File

@ -16,7 +16,7 @@ import (
"gitee.com/johng/gf/g/os/gtime"
"gitee.com/johng/gf/g/net/gipv4"
"gitee.com/johng/gf/g/net/gipv6"
"gitee.com/johng/gf/g/util/gregx"
"gitee.com/johng/gf/g/util/gregex"
"gitee.com/johng/gf/g/util/gconv"
"gitee.com/johng/gf/g/encoding/gjson"
"gitee.com/johng/gf/g/container/gmap"
@ -493,12 +493,12 @@ func Check(val interface{}, rules string, msgs interface{}, params...map[string]
// 需要判断是否被|符号截断,如果是,那么需要进行整合
for i := index + 1; i < len(items); i++ {
// 判断下一个规则是否合法,不合法那么和当前正则规则进行整合
if !gregx.IsMatchString(gSINGLE_RULE_PATTERN, items[i]) {
if !gregex.IsMatchString(gSINGLE_RULE_PATTERN, items[i]) {
ruleVal += "|" + items[i]
index++
}
}
match = gregx.IsMatchString(ruleVal, value)
match = gregex.IsMatchString(ruleVal, value)
// 日期格式,
case "date":
@ -560,19 +560,19 @@ func Check(val interface{}, rules string, msgs interface{}, params...map[string]
* 虚拟运营商170、173
*/
case "phone":
match = gregx.IsMatchString(`^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,3,5,6,7,8]{1}\d{8}$|^18[\d]{9}$`, value)
match = gregex.IsMatchString(`^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,3,5,6,7,8]{1}\d{8}$|^18[\d]{9}$`, value)
// 国内座机电话号码:"XXXX-XXXXXXX"、"XXXX-XXXXXXXX"、"XXX-XXXXXXX"、"XXX-XXXXXXXX"、"XXXXXXX"、"XXXXXXXX"
case "telephone":
match = gregx.IsMatchString(`^((\d{3,4})|\d{3,4}-)?\d{7,8}$`, value)
match = gregex.IsMatchString(`^((\d{3,4})|\d{3,4}-)?\d{7,8}$`, value)
// 腾讯QQ号从10000开始
case "qq":
match = gregx.IsMatchString(`^[1-9][0-9]{4,}$`, value)
match = gregex.IsMatchString(`^[1-9][0-9]{4,}$`, value)
// 中国邮政编码
case "postcode":
match = gregx.IsMatchString(`^[1-9]\d{5}$`, value)
match = gregex.IsMatchString(`^[1-9]\d{5}$`, value)
/*
公民身份证号
@ -596,25 +596,25 @@ func Check(val interface{}, rules string, msgs interface{}, params...map[string]
(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)
*/
case "id-number":
match = gregx.IsMatchString(`(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)`, value)
match = gregex.IsMatchString(`(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)`, value)
// 通用帐号规则(字母开头只能包含字母、数字和下划线长度在6~18之间)
case "passport":
match = gregx.IsMatchString(`^[a-zA-Z]{1}\w{5,17}$`, value)
match = gregex.IsMatchString(`^[a-zA-Z]{1}\w{5,17}$`, value)
// 通用密码(任意可见字符长度在6~18之间)
case "password":
match = gregx.IsMatchString(`^[\w\S]{6,18}$`, value)
match = gregex.IsMatchString(`^[\w\S]{6,18}$`, value)
// 中等强度密码(在弱密码的基础上,必须包含大小写字母和数字)
case "password2":
if gregx.IsMatchString(`^[\w\S]{6,18}$`, value) && gregx.IsMatchString(`[a-z]+`, value) && gregx.IsMatchString(`[A-Z]+`, value) && gregx.IsMatchString(`\d+`, value) {
if gregex.IsMatchString(`^[\w\S]{6,18}$`, value) && gregex.IsMatchString(`[a-z]+`, value) && gregex.IsMatchString(`[A-Z]+`, value) && gregex.IsMatchString(`\d+`, value) {
match = true
}
// 强等强度密码(在弱密码的基础上,必须包含大小写字母、数字和特殊字符)
case "password3":
if gregx.IsMatchString(`^[\w\S]{6,18}$`, value) && gregx.IsMatchString(`[a-z]+`, value) && gregx.IsMatchString(`[A-Z]+`, value) && gregx.IsMatchString(`\d+`, value) && gregx.IsMatchString(`\S+`, value) {
if gregex.IsMatchString(`^[\w\S]{6,18}$`, value) && gregex.IsMatchString(`[a-z]+`, value) && gregex.IsMatchString(`[A-Z]+`, value) && gregex.IsMatchString(`\d+`, value) && gregex.IsMatchString(`\S+`, value) {
match = true
}
@ -644,15 +644,15 @@ func Check(val interface{}, rules string, msgs interface{}, params...map[string]
// 邮件
case "email":
match = gregx.IsMatchString(`^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$`, value)
match = gregex.IsMatchString(`^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$`, value)
// URL
case "url":
match = gregx.IsMatchString(`^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$`, value)
match = gregex.IsMatchString(`^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$`, value)
// domain
case "domain":
match = gregx.IsMatchString(`^([0-9a-zA-Z][0-9a-zA-Z-]{0,62}\.)+([0-9a-zA-Z][0-9a-zA-Z-]{0,62})\.?$`, value)
match = gregex.IsMatchString(`^([0-9a-zA-Z][0-9a-zA-Z-]{0,62}\.)+([0-9a-zA-Z][0-9a-zA-Z-]{0,62})\.?$`, value)
// IP(IPv4/IPv6)
case "ip":
@ -668,7 +668,7 @@ func Check(val interface{}, rules string, msgs interface{}, params...map[string]
// MAC地址
case "mac":
match = gregx.IsMatchString(`^([0-9A-Fa-f]{2}-){5}[0-9A-Fa-f]{2}$`, value)
match = gregex.IsMatchString(`^([0-9A-Fa-f]{2}-){5}[0-9A-Fa-f]{2}$`, value)
default:
errorMsgs[ruleKey] = "Invalid rule name:" + ruleKey

View File

@ -3,10 +3,11 @@ package main
import (
"regexp"
"fmt"
"gitee.com/johng/gf/g/os/gtime"
)
func main() {
timeRegex, err := regexp.Compile(`(\d{4}-\d{2}-\d{2})[\sT]{0,1}(\d{2}:\d{2}:\d{2}){0,1}\.{0,1}(\d{0,9})([\sZ]{0,1})([\+-]{0,1})([:\d]*)`)
timeRegex, err := regexp.Compile(gtime.TIME_REAGEX_PATTERN)
if err != nil {
panic(err)
}