From 9d08f5f192a0e6eecf701d136f8e881a953a631a Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 26 Feb 2019 14:02:04 +0700 Subject: [PATCH] az.match('[a-f]': '/volume1', '[g-x]': '/volume2') ?: '/volume3' --- .../format/ExpressionFormatMethods.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/source/net/filebot/format/ExpressionFormatMethods.java b/source/net/filebot/format/ExpressionFormatMethods.java index b155fd99..59152bcb 100644 --- a/source/net/filebot/format/ExpressionFormatMethods.java +++ b/source/net/filebot/format/ExpressionFormatMethods.java @@ -433,13 +433,13 @@ public class ExpressionFormatMethods { } /** - * Apply multiple replacements. + * Apply replacement mappings. * * e.g. replace(ä:'ae', ö:'oe', ü:'ue') */ - public static String replace(String self, Map replace) { + public static String replace(String self, Map replacer) { // the first two parameters are required, the rest of the parameter sequence is optional - for (Entry it : replace.entrySet()) { + for (Entry it : replacer.entrySet()) { if (it.getKey() instanceof Pattern) { self = ((Pattern) it.getKey()).matcher(self).replaceAll(it.getValue().toString()); } else { @@ -449,6 +449,22 @@ public class ExpressionFormatMethods { return self; } + /** + * Find matching pattern and return mapped value. + * + * e.g. az.match('[a-f]': '/volume1', '[g-x]': '/volume2') ?: '/volume3' + */ + public static Object match(String self, Map matcher) { + // the first two parameters are required, the rest of the parameter sequence is optional + for (Entry it : matcher.entrySet()) { + Pattern p = it.getKey() instanceof Pattern ? (Pattern) it.getKey() : Pattern.compile(it.getKey().toString(), CASE_INSENSITIVE | UNICODE_CHARACTER_CLASS | MULTILINE); + if (p.matcher(self).find()) { + return it.getValue(); + } + } + return null; + } + public static String joining(Collection self, String delimiter) throws Exception { String[] list = self.stream().filter(Objects::nonNull).map(Objects::toString).filter(s -> !s.isEmpty()).toArray(String[]::new); if (list.length > 0) {