Class SafeRegex

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.regex.Pattern compileGlob​(java.lang.String globPattern)
      Compile a glob-style pattern into a Pattern.
      static java.util.regex.Pattern compileGlob​(java.lang.String globPattern, int flags)
      Compile a glob-style pattern into a Pattern with the given regex flags.
      static boolean find​(java.util.regex.Pattern pattern, java.lang.CharSequence input)
      Test whether the pattern is found anywhere in the input, with timeout protection.
      static java.util.regex.Matcher matcher​(java.util.regex.Pattern pattern, java.lang.CharSequence input)
      Create a Matcher that will throw RegexTimeoutException if matching exceeds the default timeout.
      static java.util.regex.Matcher matcher​(java.util.regex.Pattern pattern, java.lang.CharSequence input, long timeoutMs)
      Create a Matcher that will throw RegexTimeoutException if matching exceeds the given timeout.
      static boolean matches​(java.util.regex.Pattern pattern, java.lang.CharSequence input)
      Test whether the pattern matches the entire input, with timeout protection.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • matcher

        public static java.util.regex.Matcher matcher​(java.util.regex.Pattern pattern,
                                                      java.lang.CharSequence input)
        Create a Matcher that will throw RegexTimeoutException if matching exceeds the default timeout.
      • matcher

        public static java.util.regex.Matcher matcher​(java.util.regex.Pattern pattern,
                                                      java.lang.CharSequence input,
                                                      long timeoutMs)
        Create a Matcher that will throw RegexTimeoutException if matching exceeds the given timeout. The timeout starts lazily on the first deadline check during matching (not when the Matcher is created), so it measures actual matching time.
      • matches

        public static boolean matches​(java.util.regex.Pattern pattern,
                                      java.lang.CharSequence input)
        Test whether the pattern matches the entire input, with timeout protection. Returns false on timeout.
      • find

        public static boolean find​(java.util.regex.Pattern pattern,
                                   java.lang.CharSequence input)
        Test whether the pattern is found anywhere in the input, with timeout protection. Returns false on timeout.
      • compileGlob

        public static java.util.regex.Pattern compileGlob​(java.lang.String globPattern)
        Compile a glob-style pattern into a Pattern.

        Only * (match any string) and \ (escape) are special; every other character is regex-quoted so it matches literally. This is suitable for user-facing wildcard syntax where full regex power is not intended.

        Parameters:
        globPattern - the glob pattern (e.g. "foo*bar")
        Returns:
        a compiled regex pattern
      • compileGlob

        public static java.util.regex.Pattern compileGlob​(java.lang.String globPattern,
                                                          int flags)
        Compile a glob-style pattern into a Pattern with the given regex flags.
        Parameters:
        globPattern - the glob pattern
        flags - regex flags (e.g. Pattern.DOTALL)
        Returns:
        a compiled regex pattern