From 712f26309414b151ff0781083af999d3b7d049a6 Mon Sep 17 00:00:00 2001 From: Michael Langowski Date: Wed, 21 Aug 2024 21:27:52 +0200 Subject: [PATCH] Enhancement: new external to get all matches plus their bounds of a regex in a string --- .../commons/externals/AspStandardLibrary.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/alpha-commons/src/main/java/at/ac/tuwien/kr/alpha/commons/externals/AspStandardLibrary.java b/alpha-commons/src/main/java/at/ac/tuwien/kr/alpha/commons/externals/AspStandardLibrary.java index 54908ab14..72eb7a467 100644 --- a/alpha-commons/src/main/java/at/ac/tuwien/kr/alpha/commons/externals/AspStandardLibrary.java +++ b/alpha-commons/src/main/java/at/ac/tuwien/kr/alpha/commons/externals/AspStandardLibrary.java @@ -27,10 +27,9 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Set; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -261,4 +260,14 @@ public static Set>> stringHeadRemainder(String str) { return Collections.singleton(xXs); } + @Predicate(name = "regex_matches") + public static Set>> regexMatchesInString(String regex, String str) { + Matcher matcher = Pattern.compile(regex).matcher(str); // Note: This could be done more efficiently by caching patterns. + Set>> result = new LinkedHashSet<>(); + while (matcher.find()) { + result.add(List.of(Terms.newConstant(matcher.group(1)), Terms.newConstant(matcher.start(1)), Terms.newConstant(matcher.end(1)))); + } + return result; + } + }