GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #36

New issue
Copy link
New issue
Copy link
Closed
#57
Closed
Matcher checking a string matches a regular expression#36
#57
Copy link
Labels
enhancement
Milestone
2.1.0.0

Description

@Derbeth
Derbeth
opened on Jun 28, 2013
Issue body actions

I 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.

Activity

  1. lanwen commented on Jun 28, 2013

    @lanwen
    lanwen
    on Jun 28, 2013
    More actions

    Here the one (i found it on google page of hamcrest) - https://github.com/derari/cthul/wiki/Matchers#string-matchers
    Also, u can find some other here - https://github.com/yandex-qatools/matchers-java

    Reacted by Innokenty Shuvalov
  2. npryce commented on Jun 28, 2013

    @npryce
    npryce
    on Jun 28, 2013
    Contributor
    More actions

    There's also http://code.google.com/p/hamcrest-text-patterns/

  3. Derbeth commented on Jun 28, 2013

    @Derbeth
    Derbeth
    on Jun 28, 2013
    Author
    More actions

    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
  4. npryce commented on Jun 28, 2013

    @npryce
    npryce
    on Jun 28, 2013
    Contributor
    More actions

    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?

  5. Derbeth commented on Jun 29, 2013

    @Derbeth
    Derbeth
    on Jun 29, 2013
    Author
    More actions

    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.

  6. TJamesBoone commented on Dec 31, 2013

    @TJamesBoone
    TJamesBoone
    on Dec 31, 2013
    More actions

    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
  7. bacar commented on Jan 25, 2014

    @bacar
    bacar
    on Jan 25, 2014
    More actions

    Is there an objection to implementing this, or just no commits / pull requests for it?

  8. npryce commented on Jan 28, 2014

    @npryce
    npryce
    on Jan 28, 2014
    Contributor
    More actions

    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.

  9. btilford commented on Jul 30, 2014

    @btilford
    btilford
    on Jul 30, 2014
    More actions

    I end up writing one every single project I work on.

    Reacted by drdamour, Roger Pack and Yadhukrishna S Pai
  10. sf105
    closed this as completedin #57on Aug 25, 2014
  11. tomwhoiscontrary commented on Dec 18, 2014

    @tomwhoiscontrary
    tomwhoiscontrary
    on Dec 18, 2014
    More actions

    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!

  12. npathai commented on Dec 18, 2014

    @npathai
    npathai
    on Dec 18, 2014
    Contributor
    More actions

    @tomwhoiscontrary Maybe soon #65

  13. npryce
    added this to the This milestone has been deleted milestone on Dec 23, 2014
  14. npryce
    added
    enhancement
    on Dec 23, 2014
  15. jm2dev commented on Jan 28, 2015

    @jm2dev
    jm2dev
    on Jan 28, 2015
    More actions

    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
  16. alexec commented on Aug 9, 2015

    @alexec
    alexec
    on Aug 9, 2015
    More actions

    This would be awesome, one of the great thing about Hamcrest is the clear diagnostics.

    Reacted by Grzegorz Dyk and Thibault Kruse
  17. sf105 commented on Aug 9, 2015

    @sf105
    sf105
    on Aug 9, 2015
    Member
    More actions

    @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
  18. AndersDJohnson commented on Sep 2, 2015

    @AndersDJohnson
    AndersDJohnson
    on Sep 2, 2015
    More actions

    +1

  19. edbrannin commented on Nov 17, 2016

    @edbrannin
    edbrannin
    on Nov 17, 2016
    More actions

    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.

  20. tomwhoiscontrary commented on Nov 17, 2016

    @tomwhoiscontrary
    tomwhoiscontrary
    on Nov 17, 2016
    More actions

    Is Hamcrest 2 actually a thing? I thought it was an abortive sally.

  21. edbrannin commented on Nov 17, 2016

    @edbrannin
    edbrannin
    on Nov 17, 2016
    More actions

    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.

  22. rdp commented on Jan 23, 2019

    @rdp
    rdp
    on Jan 23, 2019
    More actions

    For followers, confirm there is a MatchesPattern.matchesPattern(String regex) and MatchesPattern.matchesPattern(Pattern pattern) in 2.0, ex: assertThat(myString, matchesPattern("my regex"))

    Reacted by Tom Anderson
  23. parameshjava commented on Apr 6, 2020

    @parameshjava
    parameshjava
    on Apr 6, 2020
    More actions

    matchesRegex

    There is no such method, can you please let me know which version hamcrest you are refering?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      • 2.1.0.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.