From d90b399edd894622aaef0e529c0c54004aa02a9c Mon Sep 17 00:00:00 2001 From: Tong Sun Date: Fri, 24 Dec 2021 22:01:13 -0500 Subject: [PATCH] - [+] add regular expression support --- compliance/functions.json | 12 ++++++++++++ functions.go | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/compliance/functions.json b/compliance/functions.json index 8b8db36..de46255 100644 --- a/compliance/functions.json +++ b/compliance/functions.json @@ -127,6 +127,18 @@ "expression": "contains(decimals, `false`)", "result": false }, + { + "expression": "matches('This and these', 't(his|h[eo]se)')", + "result": true + }, + { + "expression": "matches('Those people', 't(his|h[eo]se)')", + "result": false + }, + { + "expression": "matches('THOSE people', '(?i)t(his|h[eo]se)')", + "result": true + }, { "expression": "ends_with(str, 'r')", "result": true diff --git a/functions.go b/functions.go index 9b7cd89..e4d75f0 100644 --- a/functions.go +++ b/functions.go @@ -6,6 +6,7 @@ import ( "fmt" "math" "reflect" + "regexp" "sort" "strconv" "strings" @@ -169,6 +170,14 @@ func newFunctionCaller() *functionCaller { }, handler: jpfContains, }, + "matches": { + name: "matches", + arguments: []argSpec{ + {types: []jpType{jpString}}, + {types: []jpType{jpString}}, + }, + handler: jpfMatches, + }, "ends_with": { name: "ends_with", arguments: []argSpec{ @@ -458,6 +467,15 @@ func jpfContains(arguments []interface{}) (interface{}, error) { } return false, nil } +func jpfMatches(arguments []interface{}) (interface{}, error) { + search := arguments[0].(string) + rgexp := arguments[1].(string) + r, err := regexp.Compile(rgexp) + if err != nil { + return false, err + } + return r.MatchString(search), nil +} func jpfEndsWith(arguments []interface{}) (interface{}, error) { search := arguments[0].(string) suffix := arguments[1].(string)