private static boolean isMatch(String regex, String field) { try { Pattern p = PatternCache.INSTANCE.getPattern(regex); if (p == null) { return false; } return p.asPredicate().test(field); } catch(PatternSyntaxException pse) { return false; } }
@Override public Object apply(List<Object> list) { if (list.size() != 3) { throw new IllegalStateException( "REGEXP_REPLACE expects three args: [string, pattern, value]" + " where pattern is a regexp pattern"); } String str = (String) list.get(0); String stringPattern = (String) list.get(1); String value = (String) list.get(2); if (StringUtils.isEmpty(str)) { return null; } if (StringUtils.isEmpty(stringPattern) || StringUtils.isEmpty(value)) { return str; } Pattern pattern = PatternCache.INSTANCE.getPattern(stringPattern); Matcher matcher = pattern.matcher(str); return matcher.replaceAll(value); } }
@Override public Object apply(List<Object> list) { if (list.size() < 2) { throw new IllegalStateException( "REGEXP_MATCH expects two args: [string, pattern] where pattern is a regexp pattern or a list of regexp patterns"); } Object patternObject = list.get(1); String str = (String) list.get(0); if (str == null || patternObject == null) { return false; } if (patternObject instanceof String) { return PatternCache.INSTANCE.getPattern((String)patternObject).matcher(str).matches(); } else if (patternObject instanceof Iterable) { boolean matches = false; for (Object thisPatternObject : (Iterable)patternObject) { if (thisPatternObject == null) { continue; } if (PatternCache.INSTANCE.getPattern(thisPatternObject.toString()).matcher(str).matches()) { matches = true; break; } } return matches; } return false; } }
return null; Pattern pattern = PatternCache.INSTANCE.getPattern(stringPattern); Matcher matcher = pattern.matcher(str); if (!matcher.matches()) {