{"id":41,"date":"2012-01-13T12:24:12","date_gmt":"2012-01-13T11:24:12","guid":{"rendered":"http:\/\/www.flamingpenguin.co.uk\/blog\/?p=41"},"modified":"2012-01-13T12:37:19","modified_gmt":"2012-01-13T11:37:19","slug":"hamcrest-regexp-matcher","status":"publish","type":"post","link":"https:\/\/www.flamingpenguin.co.uk\/blog\/2012\/01\/13\/hamcrest-regexp-matcher\/","title":{"rendered":"Hamcrest regexp matcher"},"content":{"rendered":"<p>Matches a string against a regular expression, inspired by <a href=\"http:\/\/piotrga.wordpress.com\/2009\/03\/27\/hamcrest-regex-matcher\/\" title=\"hamcrest regexp matcher\">Piotr Gabryanczyk<\/a> with a couple of issues fixed.<\/p>\n<pre lang=\"java\">\r\nimport java.util.regex.Pattern;\r\n\r\nimport org.hamcrest.Description;\r\nimport org.hamcrest.Matcher;\r\nimport org.hamcrest.TypeSafeMatcher;\r\n\r\n\/**\r\n * Matchers that use regular expressions\r\n *\r\n * @author t.wood\r\n *\/\r\npublic class RegexMatcher\r\n{\r\n   private static abstract class AbstractRegexpMatcher extends TypeSafeMatcher<String>\r\n   {\r\n      protected final String regex;\r\n      protected final Pattern compiledRegex;\r\n\r\n      private AbstractRegexpMatcher(final String regex)\r\n      {\r\n         this.regex = regex;\r\n         compiledRegex = Pattern.compile(regex);\r\n      }\r\n   }\r\n\r\n   private static class MatchesRegexpMatcher extends AbstractRegexpMatcher\r\n   {\r\n      private MatchesRegexpMatcher(final String regex)\r\n      {\r\n         super(regex);\r\n      }\r\n\r\n      @Override\r\n      public boolean matchesSafely(final String item)\r\n      {\r\n         return compiledRegex.matcher(item).matches();\r\n      }\r\n\r\n      @Override\r\n      public void describeTo(final Description description)\r\n      {\r\n         description.appendText(\"matches regex \").appendValue(regex);\r\n      }\r\n   }\r\n\r\n   private static class ContainsMatchRegexpMatcher extends AbstractRegexpMatcher\r\n   {\r\n      private ContainsMatchRegexpMatcher(final String regex)\r\n      {\r\n         super(regex);\r\n      }\r\n\r\n      @Override\r\n      public boolean matchesSafely(final String item)\r\n      {\r\n         return compiledRegex.matcher(item).find();\r\n      }\r\n\r\n      @Override\r\n      public void describeTo(final Description description)\r\n      {\r\n         description.appendText(\"contains match for regex \").appendValue(regex);\r\n      }\r\n   }\r\n\r\n   \/**\r\n    * Match the regexp against the whole input string\r\n    *\r\n    * @param regex the regular expression to match\r\n    *\r\n    * @return a matcher which matches the whole input string\r\n    *\/\r\n   public static Matcher<String> matches(final String regex)\r\n   {\r\n      return new MatchesRegexpMatcher(regex);\r\n   }\r\n\r\n   \/**\r\n    * Match the regexp against any substring of the input string\r\n    *\r\n    * @param regex the regular expression to match\r\n    *\r\n    * @return a matcher which matches anywhere in the input string\r\n    *\/\r\n   public static Matcher<String> containsMatch(final String regex)\r\n   {\r\n      return new ContainsMatchRegexpMatcher(regex);\r\n   }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Matches a string against a regular expression, inspired by Piotr Gabryanczyk with a couple of issues fixed. import java.util.regex.Pattern; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; \/** * Matchers that use regular expressions * * @author t.wood *\/ public class RegexMatcher &hellip; <a href=\"https:\/\/www.flamingpenguin.co.uk\/blog\/2012\/01\/13\/hamcrest-regexp-matcher\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":0,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flamingpenguin.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}