Class RegexMatchers


  • public final class RegexMatchers
    extends Object
    Convenient matchers for checking Strings against regular expressions.
    Since:
    1.3
    • Method Detail

      • matchesAnyPattern

        public static org.hamcrest.Matcher<String> matchesAnyPattern​(String... patterns)
        Checks whether a String matches at lease one of given regular expressions.
        Parameters:
        patterns - Regular expression patterns
        Returns:
        Matcher suitable for JUnit/Hamcrest matching
      • matchesPattern

        public static org.hamcrest.Matcher<String> matchesPattern​(String pattern)
        Checks whether a String matches the given regular expression. Works in a similar manner to String.matches(String). For example:
         MatcherAssert.assert(
           "abc123",
           RegexMatchers.matchesPattern("[a-c]+\\d{3}")
         );
        Parameters:
        pattern - The pattern to match against
        Returns:
        Matcher suitable for JUnit/Hamcrest matching
      • containsPattern

        public static org.hamcrest.Matcher<String> containsPattern​(String pattern)
        Checks whether a String contains a subsequence matching the given regular expression. Works in a similar manner to Matcher.find(). For example:
         MatcherAssert.assert(
           "fooBar123",
           RegexMatchers.containsPattern("Bar12")
         );
        Parameters:
        pattern - The pattern to match against
        Returns:
        Matcher suitable for JUnit/Hamcrest matching
      • containsAnyPattern

        public static org.hamcrest.Matcher<String> containsAnyPattern​(String... patterns)
        Checks whether a String contains a subsequence matching any of the given regular expressions.
        Parameters:
        patterns - The patterns to match against
        Returns:
        Matcher suitable for JUnit/Hamcrest matching
        See Also:
        Matcher.find(), containsPattern(String)
      • containsAllPatterns

        public static org.hamcrest.Matcher<String> containsAllPatterns​(String... patterns)
        Checks whether a String contains a subsequence matching any of the given regular expressions.
        Parameters:
        patterns - The patterns to match against
        Returns:
        Matcher suitable for JUnit/Hamcrest matching
        See Also:
        Matcher.find(), containsPattern(String)