Class RegexUtils

java.lang.Object
org.keycloak.utils.RegexUtils

public class RegexUtils extends Object
Utility methods for validating and matching regular expressions.
  • Field Details

    • DEFAULT_MAX_LENGTH

      public static final int DEFAULT_MAX_LENGTH
      Default maximum regex length — limits complexity to mitigate ReDoS attacks.
      See Also:
  • Constructor Details

    • RegexUtils

      public RegexUtils()
  • Method Details

    • isValidRegex

      public static boolean isValidRegex(String regexp)
      Validates whether the given string is a syntactically valid regular expression with a maximum length of DEFAULT_MAX_LENGTH and groups allowed.
      Parameters:
      regexp - the regular expression to validate
      Returns:
      true if the expression is valid, false otherwise
    • isValidRegex

      public static boolean isValidRegex(String regexp, int maxLength, boolean allowGroups)
      Validates whether the given string is a syntactically valid regular expression.
      Parameters:
      regexp - the regular expression to validate
      maxLength - the maximum allowed length of the expression
      allowGroups - whether capturing and non-capturing groups (parentheses) are permitted
      Returns:
      true if the expression is valid, false if it is null, exceeds maxLength, contains groups when disallowed, or has invalid syntax
    • valueMatchesRegex

      public static boolean valueMatchesRegex(String regex, Object value)