package retromock.matchers;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

import java.util.regex.Pattern;

public class IsRegex extends TypeSafeDiagnosingMatcher<String> {

    private final Pattern pattern;

    public static IsRegex matchesRegex(String regex) {
        return new IsRegex(Pattern.compile(regex));
    }

    public IsRegex(Pattern pattern) {
        this.pattern = pattern;
    }

    @Override
    protected boolean matchesSafely(String item, Description mismatchDescription) {
        return pattern.matcher(item).matches();
    }

    @Override
    public void describeTo(Description description) {
        description
                .appendText("a string matching the pattern '")
                .appendValue(pattern)
                .appendText("'");
    }
}

package retromock.matchers;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

import java.util.regex.Pattern;

public class IsRegex extends TypeSafeDiagnosingMatcher<String> {

    private final Pattern pattern;

    public static IsRegex matchesRegex(String regex) {
        return new IsRegex(Pattern.compile(regex));
    }

    public IsRegex(Pattern pattern) {
        this.pattern = pattern;
    }

    @Override
    protected boolean matchesSafely(String item, Description mismatchDescription) {
        final boolean matches = pattern.matcher(item).matches();
        if (!matches) {
            mismatchDescription
                    .appendText("Item ")
                    .appendValue(item)
                    .appendText(" did not match pattern ")
                    .appendValue(pattern);
        }
        return matches;
    }

    @Override
    public void describeTo(Description description) {
        description
                .appendText("a string matching the pattern '")
                .appendValue(pattern)
                .appendText("'");
    }
}

Add describing text to mismatchDescription in case on mismatch