Skip to content

Commit

Permalink
- [+] add regular expression support
Browse files Browse the repository at this point in the history
  • Loading branch information
suntong committed Dec 25, 2021
1 parent 17482ba commit d90b399
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions compliance/functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d90b399

Please sign in to comment.