Class MatcherUtil


  • public final class MatcherUtil
    extends java.lang.Object
    Utility helping to avoid ReDoS, StackOverflowError, etc during regex matching. Potentially harmful regular expressions are the ones with repeated groups which have either repetition or alternation with overlapping inside
    • Constructor Summary

      Constructors 
      Constructor Description
      MatcherUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.regex.Matcher createMatcherWithTimeout​(java.lang.String input, java.util.regex.Pattern pattern, int timeout)  
      static java.lang.String find​(java.lang.String input, java.lang.String regex)  
      static java.lang.String find​(java.lang.String input, java.lang.String regex, int timeout)
      Attempts to find a subsequence of the provided input sequence that matches the provided pattern.
      static java.lang.String find​(java.lang.String input, java.util.regex.Pattern pattern)
      Uses default timeout specified by REGEX_MATCHING_TIMEOUT_MS internal property or default timeout specified by DEFAULT_REGEX_MATCHING_TIMEOUT_MS or falls back to regular matching without timeout in case of negative REGEX_MATCHING_TIMEOUT_MS internal property value
      static java.lang.String find​(java.lang.String input, java.util.regex.Pattern pattern, int timeout)
      Same as the above method, but accepts compiled Pattern
      static java.lang.String find​(java.util.regex.Matcher matcher)  
      static java.lang.String find​(java.util.regex.Matcher matcher, int start)  
      static boolean matches​(java.lang.String input, java.lang.String regex, int timeout)
      Match the provided input against the provided regular expression.
      static boolean matches​(java.lang.String input, java.util.regex.Pattern pattern, int timeout)
      Match the provided input against the provided regular expression.
      • Methods inherited from class java.lang.Object

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

      • REGEX_MATCHING_TIMEOUT_MS

        public static final java.lang.String REGEX_MATCHING_TIMEOUT_MS
        See Also:
        Constant Field Values
      • DEFAULT_REGEX_MATCHING_TIMEOUT_MS

        public static final int DEFAULT_REGEX_MATCHING_TIMEOUT_MS
        See Also:
        Constant Field Values
    • Constructor Detail

      • MatcherUtil

        public MatcherUtil()
    • Method Detail

      • find

        @Nullable
        public static java.lang.String find​(@NotNull
                                            java.lang.String input,
                                            @NotNull
                                            java.lang.String regex,
                                            int timeout)
                                     throws MatcherUtil.MatchingException
        Attempts to find a subsequence of the provided input sequence that matches the provided pattern. Throw exception in case the matching takes more than timeout milliseconds.
        Parameters:
        input - search for match in
        regex - searched pattern
        timeout - milliseconds
        Returns:
        see above
        Throws:
        MatcherUtil.MatchingException - see above
      • find

        @Nullable
        public static java.lang.String find​(@NotNull
                                            java.util.regex.Matcher matcher,
                                            int start)
      • matches

        public static boolean matches​(@NotNull
                                      java.lang.String input,
                                      @NotNull
                                      java.lang.String regex,
                                      int timeout)
                               throws MatcherUtil.MatchingException
        Match the provided input against the provided regular expression. Throw exception in case the matcing takes more than timeout milliseconds.
        Throws:
        MatcherUtil.MatchingException
      • matches

        public static boolean matches​(@NotNull
                                      java.lang.String input,
                                      @NotNull
                                      java.util.regex.Pattern pattern,
                                      int timeout)
                               throws MatcherUtil.MatchingException
        Match the provided input against the provided regular expression. Return false in case the matcing takes more than timeout milliseconds.
        Throws:
        MatcherUtil.MatchingException
      • createMatcherWithTimeout

        @NotNull
        public static java.util.regex.Matcher createMatcherWithTimeout​(@NotNull
                                                                       java.lang.String input,
                                                                       @NotNull
                                                                       java.util.regex.Pattern pattern,
                                                                       int timeout)