Lookahead based regex

This commit is contained in:
dotasek 2023-06-13 17:30:33 -04:00
parent 3c7abb5eff
commit 1a14bf3ef8
1 changed files with 6 additions and 3 deletions

View File

@ -6,15 +6,18 @@ import java.io.IOException;
public class AsteriskFilter implements FilenameFilter {
public static final String EXPRESSION_REGEX = "(.+(?>\\\\|/))*(.*)";
public static final String DIR_REGEX = EXPRESSION_REGEX + "\\*(.*)";
String dir;
String regex;
public AsteriskFilter(String filter) throws IOException {
if (!filter.matches("(.*(\\\\|\\/))*(.*)\\*(.*)"))
if (!filter.matches(DIR_REGEX))
throw new IOException("Filter names must have the following syntax: [directorypath][prefix]?*[suffix]? I.e. The asterisk must be in the filename, not the directory path");
dir = filter.replaceAll("(.*(\\\\|\\/))*(.*)\\*(.*)", "$1");
String expression = filter.replaceAll("(.*(\\\\|\\/))*(.*)", "$3");
dir = filter.replaceAll(DIR_REGEX, "$1");
String expression = filter.replaceAll(AsteriskFilter.EXPRESSION_REGEX, "$2");
regex = "";
for (int i = 0; i < expression.length(); i++) {
if (Character.isAlphabetic(expression.codePointAt(i)) || Character.isDigit(expression.codePointAt(i)))