Skip to content

Commit 5e51a70

Browse files
committed
Added string regex matcher
1 parent 56c5bbb commit 5e51a70

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
*
3+
*/
4+
package org.hamcrest.core;
5+
6+
import java.util.regex.Pattern;
7+
8+
import org.hamcrest.Description;
9+
import org.hamcrest.Matcher;
10+
import org.hamcrest.TypeSafeDiagnosingMatcher;
11+
12+
/**
13+
* @author borettim
14+
* @author sf105
15+
*
16+
*/
17+
public class StringRegularExpression extends TypeSafeDiagnosingMatcher<String> {
18+
19+
protected StringRegularExpression(Pattern pattern) {
20+
this.pattern = pattern;
21+
}
22+
23+
private Pattern pattern;
24+
25+
@Override
26+
public void describeTo(Description description) {
27+
description.appendText("a string matching the pattern ").appendValue(pattern);
28+
}
29+
30+
@Override
31+
protected boolean matchesSafely(String actual, Description mismatchDescription) {
32+
if (!pattern.matcher(actual).matches()) {
33+
mismatchDescription.appendText("the string was ").appendValue(actual);
34+
return false;
35+
}
36+
return true;
37+
}
38+
39+
/**
40+
* Validate a string with a {@link java.util.regex.Pattern}.
41+
*
42+
* <pre>
43+
* assertThat(&quot;abc&quot;, matchesRegex(Pattern.compile(&quot;&circ;[a-z]$&quot;));
44+
* </pre>
45+
*
46+
* @param pattern
47+
* the pattern to be used.
48+
* @return The matcher.
49+
*/
50+
public static Matcher<String> matchesRegex(Pattern pattern) {
51+
return new StringRegularExpression(pattern);
52+
}
53+
54+
/**
55+
* Validate a string with a regex.
56+
*
57+
* <pre>
58+
* assertThat(&quot;abc&quot;, matchesRegex(&quot;&circ;[a-z]+$&quot;));
59+
* </pre>
60+
*
61+
* @param regex
62+
* The regex to be used for the validation.
63+
* @return The matcher.
64+
*/
65+
public static Matcher<String> matchesRegex(String regex) {
66+
return matchesRegex(Pattern.compile(regex));
67+
}
68+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.hamcrest.core;
2+
3+
import org.hamcrest.AbstractMatcherTest;
4+
import org.hamcrest.Matcher;
5+
6+
import static org.hamcrest.core.StringRegularExpression.matchesRegex;
7+
8+
/**
9+
* @author Steve Freeman 2016 http://www.hamcrest.com
10+
*/
11+
public class StringRegularExpressionTest extends AbstractMatcherTest {
12+
public final Matcher<String> matcher = matchesRegex("^[0-9]+$");
13+
14+
@Override
15+
protected Matcher<?> createMatcher() { return matcher; }
16+
17+
18+
public void testMatchingRegex() {
19+
assertMatches(matcher, "12");
20+
assertDoesNotMatch(matcher, "abc");
21+
22+
assertDescription("a string matching the pattern <^[0-9]+$>", matcher);
23+
assertMismatchDescription("the string was \"bcd\"", matcher, "bcd");
24+
}
25+
}

hamcrest-library/src/main/java/org/hamcrest/Matchers.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.hamcrest;
22

33
import org.hamcrest.collection.ArrayMatching;
4+
import org.hamcrest.core.StringRegularExpression;
5+
6+
import java.util.regex.Pattern;
47

58
@SuppressWarnings("unused")
69
public class Matchers {
@@ -536,6 +539,36 @@ public static org.hamcrest.Matcher<java.lang.String> endsWithIgnoringCase(java.l
536539
return org.hamcrest.core.StringEndsWith.endsWithIgnoringCase(suffix);
537540
}
538541

542+
/**
543+
* Validate a string with a {@link java.util.regex.Pattern}.
544+
*
545+
* <pre>
546+
* assertThat(&quot;abc&quot;, matchesRegex(Pattern.compile(&quot;&circ;[a-z]$&quot;));
547+
* </pre>
548+
*
549+
* @param pattern
550+
* the pattern to be used.
551+
* @return The matcher.
552+
*/
553+
public static Matcher<String> matchesRegex(Pattern pattern) {
554+
return StringRegularExpression.matchesRegex(pattern);
555+
}
556+
557+
/**
558+
* Validate a string with a regex.
559+
*
560+
* <pre>
561+
* assertThat(&quot;abc&quot;, matchesRegex(&quot;&circ;[a-z]+$&quot;));
562+
* </pre>
563+
*
564+
* @param regex
565+
* The regex to be used for the validation.
566+
* @return The matcher.
567+
*/
568+
public static Matcher<String> matchesRegex(String regex) {
569+
return StringRegularExpression.matchesRegex(Pattern.compile(regex));
570+
}
571+
539572
/**
540573
* Creates a matcher that matches arrays whose elements are satisfied by the specified matchers. Matches
541574
* positively only if the number of matchers specified is equal to the length of the examined array and

0 commit comments

Comments
 (0)