Piotr Gabryanczyk’s Blog

Java, Refactoring, AOP, Spring, DDD, TDD, etc.

  • Blogroll

    • I have joined Anti-IF Campaign

Hamcrest Regex Matcher

Posted by Piotr Gabryanczyk on March 27, 2009

Problem

I could not find a regex matcher in hamcrest, to do ie.
assertThat(selenium.getTitle(), matches("Template T\d{3}"));

It could be that I was not looking well enough or Nat Pryce decided not to include it on purpose. Of’course I saw PatternMatcher, but it lets you build regexes rather then match against them.

Solution

So here you have, enjoy!


public class RegexMatcher extends BaseMatcher{
    private final String regex;

    public RegexMatcher(String regex){
        this.regex = regex;
    }

    public boolean matches(Object o){
        return ((String)o).matches(regex);

    }

    public void describeTo(Description description){
        description.appendText("matches regex=");
    }
    
    public static RegexMatcher matches(String regex){
        return new RegexMatcher(regex);
    }
}

Advertisements

7 Responses to “Hamcrest Regex Matcher”

  1. twoj_kolega said

    Nice once, just what i needed today.
    This is currently number 2 on google for “hamcrest regex matcher”

  2. twoj_kolega said

    Make it type-safe by changing class definition to:

    public class RegexMatcher extends BaseMatcher {
    
  3. twoj_kolega said

    Damn html, i mean’t: public class RegexMatcher extends BaseMatcher<String>

  4. If you use Mockito you could also try:


    import org.hamcrest.Matcher;
    import org.mockito.internal.matchers.Matches;

    public class RegexMatcher {

    public static Matcher matchesRegex(String regex){
    return new Matches(regex);
    }

    }

    There is a static already called matches in mockito but unfortunately it is used for argument matching and so returns a string.

  5. Jeff Walker said

    import java.util.regex.Pattern;
    
    import org.hamcrest.Description;
    import org.hamcrest.Factory;
    import org.hamcrest.Matcher;
    import org.hamcrest.TypeSafeMatcher;
    
    public class RegularExpressionMatcher extends TypeSafeMatcher {
    	
    	private final Pattern pattern;
    
    	public RegularExpressionMatcher(String pattern) {
    		this(Pattern.compile(pattern));
    	}
    	public RegularExpressionMatcher(Pattern pattern) {
    		this.pattern = pattern;
    	}
    
    	@Override
    	public void describeTo(Description description) {
    		description.appendText("matches regular expression ").appendValue(pattern);
    	}
    
    	@Override
    	public boolean matchesSafely(String item) {
    		return pattern.matcher(item).matches();
    	}
    
    
    	@Factory
    	public static Matcher matchesPattern(Pattern pattern) {
    		return new RegularExpressionMatcher(pattern);
    	}
    	@Factory
    	public static Matcher matchesPattern(String pattern) {
    		return new RegularExpressionMatcher(pattern);
    	}
    }
    
  6. […] a string against a regular expression, inspired by Piotr Gabryanczyk with a couple of issues fixed. import org.hamcrest.Description; import […]

  7. Thanks, very useful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: