Class RegexUtil

java.lang.Object
org.plumelib.util.RegexUtil

@AnnotatedFor("nullness") public final class RegexUtil extends Object
Utility methods for regular expressions, most notably for testing whether a string is a regular expression.

For an example of intended use, see section Testing whether a string is a regular expression in the Checker Framework manual.

  • Method Details

    • isRegex

      @Pure @EnsuresQualifierIf(result=true, expression="#1", qualifier=org.checkerframework.checker.regex.qual.Regex.class) public static boolean isRegex(String s)
      Returns true if the argument is a syntactically valid regular expression.
      Parameters:
      s - string to check for being a regular expression
      Returns:
      true iff s is a regular expression
    • isRegex

      @Pure @EnsuresQualifierIf(result=true, expression="#1", qualifier=org.checkerframework.checker.regex.qual.Regex.class) public static boolean isRegex(String s, int groups)
      Returns true if the argument is a syntactically valid regular expression with at least the given number of groups.
      Parameters:
      s - string to check for being a regular expression
      groups - number of groups expected
      Returns:
      true iff s is a regular expression with groups groups
    • isRegex

      @Pure @EnsuresQualifierIf(result=true, expression="#1", qualifier=org.checkerframework.checker.regex.qual.Regex.class) public static boolean isRegex(char c)
      Returns true if the argument is a syntactically valid regular expression.
      Parameters:
      c - char to check for being a regular expression
      Returns:
      true iff c is a regular expression
    • asRegex

      @SideEffectFree public static @Regex String asRegex(String s)
      Returns the argument as a @Regex String if it is a regex, otherwise throws an error. The purpose of this method is to suppress Regex Checker warnings. It should be very rarely needed.
      Parameters:
      s - string to check for being a regular expression
      Returns:
      its argument
      Throws:
      Error - if argument is not a regex
    • asRegex

      @SideEffectFree public static @Regex String asRegex(String s, int groups)
      Returns the argument as a @Regex(groups) String if it is a regex with at least the given number of groups, otherwise throws an error. The purpose of this method is to suppress Regex Checker warnings. It should be very rarely needed.
      Parameters:
      s - string to check for being a regular expression
      groups - number of groups expected
      Returns:
      its argument
      Throws:
      Error - if argument is not a regex
    • regexError

      @SideEffectFree public static @Nullable String regexError(String s)
      Returns null if the argument is a syntactically valid regular expression. Otherwise returns a string describing why the argument is not a regex.
      Parameters:
      s - string to check for being a regular expression
      Returns:
      null, or a string describing why the argument is not a regex
    • regexError

      @SideEffectFree public static @Nullable String regexError(String s, int groups)
      Returns null if the argument is a syntactically valid regular expression with at least the given number of groups. Otherwise returns a string describing why the argument is not a regex.
      Parameters:
      s - string to check for being a regular expression
      groups - number of groups expected
      Returns:
      null, or a string describing why the argument is not a regex
    • regexException

      @SideEffectFree public static @Nullable PatternSyntaxException regexException(String s)
      Returns null if the argument is a syntactically valid regular expression. Otherwise returns a PatternSyntaxException describing why the argument is not a regex.
      Parameters:
      s - string to check for being a regular expression
      Returns:
      null, or a PatternSyntaxException describing why the argument is not a regex
    • regexException

      @SideEffectFree public static @Nullable PatternSyntaxException regexException(String s, int groups)
      Returns null if the argument is a syntactically valid regular expression with at least the given number of groups. Otherwise returns a PatternSyntaxException describing why the argument is not a regex.
      Parameters:
      s - string to check for being a regular expression
      groups - number of groups expected
      Returns:
      null, or a PatternSyntaxException describing why the argument is not a regex
    • matchesSomeRegex

      public static List<String> matchesSomeRegex(Collection<String> strings, Collection<@Regex String> regexes)
      Return the strings such that any one of the regexes matches it.
      Parameters:
      strings - a collection of strings
      regexes - a collection of regular expressions
      Returns:
      the strings such that any one of the regexes matches it
    • everyStringMatchesSomeRegex

      public static boolean everyStringMatchesSomeRegex(Collection<String> strings, Collection<@Regex String> regexes)
      Return true if every string is matched by at least one regex.
      Parameters:
      strings - a collection of strings
      regexes - a collection of regular expressions
      Returns:
      true if every string is matched by at least one regex
    • matchesNoRegex

      public static List<String> matchesNoRegex(Collection<String> strings, Collection<@Regex String> regexes)
      Return the strings that are matched by no regex.
      Parameters:
      strings - a collection of strings
      regexes - a collection of regular expressions
      Returns:
      the strings such that none of the regexes matches it
    • noStringMatchesAnyRegex

      public static boolean noStringMatchesAnyRegex(Collection<String> strings, Collection<@Regex String> regexes)
      Return true if no string is matched by any regex.
      Parameters:
      strings - a collection of strings
      regexes - a collection of regular expressions
      Returns:
      true if no string is matched by any regex