添加正则表达式的方法

This commit is contained in:
eson 2018-12-23 21:36:09 +08:00
parent fdedd9ddb2
commit 013383b188

View File

@ -5,6 +5,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"log"
"reflect" "reflect"
"regexp" "regexp"
"strconv" "strconv"
@ -744,6 +745,7 @@ func parseArrayPath(path string) (r arrayPathResult) {
path[i] == '<' || path[i] == '<' ||
path[i] == '>' || path[i] == '>' ||
path[i] == '%' || path[i] == '%' ||
path[i] == '~' ||
path[i] == ']' { path[i] == ']' {
break break
} }
@ -779,38 +781,66 @@ func parseArrayPath(path string) (r arrayPathResult) {
break break
} }
} }
s = i s = i + 1
for ; i < len(path); i++ {
if path[i] == '"' { if r.query.op == "~" {
i++ pair := path[i]
s2 := i i++
for ; i < len(path); i++ { GETREGEXPSTR:
if path[i] > '\\' { for ; i < len(path); i++ {
continue if path[i] == pair {
} for iend := i + 1; iend < len(path); iend++ {
if path[i] == '"' {
// look for an escaped slash if path[iend] == ' ' {
if path[i-1] == '\\' { continue
n := 0
for j := i - 2; j > s2-1; j-- {
if path[j] != '\\' {
break
}
n++
}
if n%2 == 0 {
continue
}
} }
break
if path[iend] == ']' {
if iend+1 < len(path) && path[iend+1] == '#' {
r.query.all = true
}
break GETREGEXPSTR
} else {
break
}
} }
} }
} else if path[i] == ']' {
if i+1 < len(path) && path[i+1] == '#' {
r.query.all = true
}
break
} }
} else {
for ; i < len(path); i++ {
if path[i] == '"' {
i++
s2 := i
for ; i < len(path); i++ {
if path[i] > '\\' {
continue
}
if path[i] == '"' {
// look for an escaped slash
if path[i-1] == '\\' {
n := 0
for j := i - 2; j > s2-1; j-- {
if path[j] != '\\' {
break
}
n++
}
if n%2 == 0 {
continue
}
}
break
}
}
} else if path[i] == ']' {
if i+1 < len(path) && path[i+1] == '#' {
r.query.all = true
}
break
}
} //
} }
if i > len(path) { if i > len(path) {
i = len(path) i = len(path)
@ -1117,15 +1147,15 @@ func queryMatches(rp *arrayPathResult, value Result) bool {
case ">=": case ">=":
return value.Str >= rpv return value.Str >= rpv
case "%": case "%":
return match.Match(value.Str, rpv)
case "!%":
return !match.Match(value.Str, rpv)
case "~":
log.Println(rpv)
if rp.re == nil { if rp.re == nil {
rp.re = regexp.MustCompile(escapeRegexpString(rpv)) rp.re = regexp.MustCompile(rpv)
} }
return rp.re.MatchString(value.Str) return rp.re.MatchString(value.Str)
case "!%":
if rp.re == nil {
rp.re = regexp.MustCompile(escapeRegexpString(rpv))
}
return !rp.re.MatchString(value.Str)
} }
case Number: case Number:
rpvn, _ := strconv.ParseFloat(rpv, 64) rpvn, _ := strconv.ParseFloat(rpv, 64)