Skip to content

Commit

Permalink
Enhancement: new external to get all matches plus their bounds of a r…
Browse files Browse the repository at this point in the history
…egex in a string
  • Loading branch information
madmike200590 committed Aug 21, 2024
1 parent 119e2b1 commit 712f263
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -261,4 +260,14 @@ public static Set<List<ConstantTerm<String>>> stringHeadRemainder(String str) {
return Collections.singleton(xXs);
}

@Predicate(name = "regex_matches")
public static Set<List<ConstantTerm<?>>> regexMatchesInString(String regex, String str) {
Matcher matcher = Pattern.compile(regex).matcher(str); // Note: This could be done more efficiently by caching patterns.
Set<List<ConstantTerm<?>>> 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;
}

}

0 comments on commit 712f263

Please sign in to comment.