Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -363,17 +363,16 @@ Boolean evalOperatorCondition(String operatorString, @Nullable JsonElement actua
break;

case REGEX:
if (actual == null || DataType.NULL.equals(attributeDataType)) return false;
Pattern pattern = Pattern.compile(expected.getAsString());
Matcher matcher = pattern.matcher(actual.getAsString());
return evalRegex(actual, expected, attributeDataType, false, false);

boolean matches = false;
case REGEX_I:
return evalRegex(actual, expected, attributeDataType, true, false);

while (matcher.find()) {
matches = true;
}
case NOT_REGEX:
return evalRegex(actual, expected, attributeDataType, false, true);

return matches;
case NOT_REGEX_I:
return evalRegex(actual, expected, attributeDataType, true, true);

case NE:
if (DataType.NULL.equals(attributeDataType)) return false;
Expand Down Expand Up @@ -681,4 +680,23 @@ private <T> boolean isMatchingPrimitive(
.equals(extractor.apply(
attributeValue.getAsJsonPrimitive()));
}

private static Boolean evalRegex(@Nullable JsonElement actual,
JsonElement expected,
DataType attributeDataType,
boolean caseInsensitive,
boolean negate) {
if (actual == null || DataType.NULL.equals(attributeDataType)) return false;

int flags = caseInsensitive ? Pattern.CASE_INSENSITIVE : 0;

try {
Pattern pattern = Pattern.compile(expected.getAsString(), flags);
Matcher matcher = pattern.matcher(actual.getAsString());
boolean matches = matcher.find();
return negate != matches;
} catch (Exception e) {
return false;
}
}
}
12 changes: 12 additions & 0 deletions lib/src/main/java/growthbook/sdk/java/model/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ public enum Operator {
* $regex
*/
REGEX("$regex"),
/**
* $regexi
*/
REGEX_I("$regexi"),
/**
* $notRegex
*/
NOT_REGEX("$notRegex"),
/**
* $notRegexi
*/
NOT_REGEX_I("$notRegexi"),
/**
* $ne
*/
Expand Down
Loading
Loading