GeezerVison is showing github.com — Matcher checking a string matches a regular expression · Issue #36 · hamcrest/JavaHamcrest
Matcher checking a string matches a regular expression #36DescriptionI cannot find any class allowing me to write my tests like:
String actual = tested.buildUrlWithRandomPort("foo", "bar");
assertThat(actual, matchesRegex("^foo://bar:\\d+/$");
// fails with a message like:
// expected <foo://bar:/> to match a regular expression <^foo://bar:\d+/$>
I think that Hamcrest Library should offer matchesReges() together with containsString(), endsWith() and startsWith().
Is there a matcher like this available in the master branch? If not, I could write one (something similar to Hamcrest Regex Matcher) and create a pull request. Reacted by André Pinto and Daniel M. Activitylanwen commented on Jun 28, 2013 Reacted by Innokenty Shuvalov npryce commented on Jun 28, 2013 Derbeth commented on Jun 28, 2013 hamcrest-text-patterns does the opposite thing than I asked for.
Ok, cthul looks nice, however I think that matching against a regex is such a core functionality that you should not require users to use third-party libraries. This would be the same bizzare situation as with C++ failing to provide a standard regex implementation. I think that when you write some test code, you constantly come across writing assertions making use of regular expressions. See how RSpec constructed its documentations: regexes are emphasised there.
As cthul is MIT-licensed, I suggest you copy the regex-related classes to hamcrest-library, adding cthul authors to the contributor list (to fulfil the legal licence requirements). Reacted by Asanga K and tkrishnakishore npryce commented on Jun 28, 2013 In what way does hamcrest-text-patterns do the opposite thing to what you asked for? It is a hamcrest matcher that matches strings against regular expressions. What do you want? Derbeth commented on Jun 29, 2013 Ok, you are right, the section "Matching" shows regex-like matching, however, if I understand correctly, you need to use library builders to construct a matcher and you are not allowed to use normal regular expressions given as strings. I find this way of writing assertions way to verbose in many cases. TJamesBoone commented on Dec 31, 2013 Derbeth is right. Hamcrest ought to have a simple, non-verbose regex matcher. There are many good reasons to and no good reasons not to.
The point is, I should be able write something like this:
Matcher<String> regexMatcher = Matchers.matchesRegex([insert regex expression here]);
Quick. Easy. Simple. No third party integrations required, no matter how clean. Reacted by Roger Pack and Daniel Sergeev bacar commented on Jan 25, 2014 Is there an objection to implementing this, or just no commits / pull requests for it? npryce commented on Jan 28, 2014 No objection. There used to be a RegexMatcher in Hamcrest but it got removed because it made supporting different JDK versions difficult. But that was a very long time ago; Hamcrest no longer supports JDK versions without java.util.Pattern. btilford commented on Jul 30, 2014 I end up writing one every single project I work on. Reacted by drdamour, Roger Pack and Yadhukrishna S Pai tomwhoiscontrary commented on Dec 18, 2014 Any idea when there might be a release with this matcher in? So far, i've copied and pasted it into two projects, and i'm going to die of shame if i have to do it again! npathai commented on Dec 18, 2014 added this to the This milestone has been deleted milestone on Dec 23, 2014jm2dev commented on Jan 28, 2015 Assuming there is no matcher for regular expressions, you may consider to rewrite your assertion to something like this:
assertThat("Expected result matches regular expression",
expected.matches(regularExpression),
is(true));Reacted by Nate Barbettini, Filipe Castilho, Roxxy, ortang, Guilherme Gonçalves and Walter Gómez alexec commented on Aug 9, 2015 This would be awesome, one of the great thing about Hamcrest is the clear diagnostics. Reacted by Grzegorz Dyk and Thibault Kruse sf105 commented on Aug 9, 2015 @jm2dev I don't understand your point. Where does the actual value go? Also, if it fails, you just get an expected: true, was: false. Reacted by Eugene Kirpichov and Joël Schaal AndersDJohnson commented on Sep 2, 2015 edbrannin commented on Nov 17, 2016 It turns out this was released as part of java-hamcrest 2.0.0.0 -- you'll also want to add hamcrest-junit to your project if you upgrade from 1.3, and use its assertThat() instead of the one in jUnit 4. tomwhoiscontrary commented on Nov 17, 2016 Is Hamcrest 2 actually a thing? I thought it was an abortive sally. edbrannin commented on Nov 17, 2016 There doesn't seem to be much documentation, but it's published to Maven Central. I was able to put it & the new JUnit integration in pom.xml and get my project building.
The main issue was I couldn't import static org.junit.Assert.*; anymore (for older test cases that still use assertEquals, etc.) because the two different assertThat() implementations got ambiguous. rdp commented on Jan 23, 2019 For followers, confirm there is a MatchesPattern.matchesPattern(String regex) and MatchesPattern.matchesPattern(Pattern pattern) in 2.0, ex: assertThat(myString, matchesPattern("my regex")) parameshjava commented on Apr 6, 2020
matchesRegex
There is no such method, can you please let me know which version hamcrest you are refering?
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|