package enea.lecservice.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {
    public static final String REGEX_SIMPLE_TEXT = "[0-9a-zA-Z_\\.]+";
    public static boolean matchesRegex(String input, String regex) {
        // Compile the regex pattern
        Pattern pattern = Pattern.compile(regex);

        // Match the input string against the regex pattern
        Matcher matcher = pattern.matcher(input);

        // Return whether the input string matches the regex pattern
        return matcher.matches();
    }

    public static String validRegex(String s, String regex) throws Exception {
        if (matchesRegex(s, regex))
            return s;
        throw new Exception("Provided input is not allowed.");
    }

    public static String validSimpleText(String s) throws Exception {
        if (matchesRegex(s, REGEX_SIMPLE_TEXT))
            return s;
        throw new Exception("Provided input is not allowed.");
    }
}
