question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

StringAssertions - Count regex matches

See original GitHub issue

Description

I am using the FA string assertions MatchRegex() to check strings. But because the regex pattern have to be really generic to match all future strings, it turned out to be a test case that will mostly ever pass (and therefore is useless).

Now if I could restrict the assertion to a predictable count of regex matches, this could be (for me) a better solution.

Complete minimal example reproducing the issue


class A
{
    public string LongLongString = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est (100,00€)Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore (104,23€)magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
}

[Fact]
public void Test_Regex_matches()
{
    // Arrange
    var a = new A();
    
    // Act / Assert
    // Matching e.g. "(100,00€)"
    a.LongLongString.Should().MatchRegex(@"(.*\(\d+,\d{2}€\))+", Exactly.Twice());
}

Expected behavior:

I think an overload like the string.Contains("bla", AtLeast.Once()) should be good, so: a.LongLongString().Should().MatchRegex("bla", Exactly.Twice());

Actual behavior:

Cannot be done (except I am missing some way to achieve this)

Versions

  • Which version of Fluent Assertions are you using? -> 6.4.0
  • Which .NET runtime and version are you targeting? .NET Core 3.1

Additional Information

Unfortunately string.Contains("bla", AtLeast.Once()) and similar assertions can not be done, because I am trying to match price values inside the text.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ITaluonecommented, Mar 28, 2022

Ok, thank you

I will take that for the moment to be able to go further in development. Helps a lot 😃

0reactions
jnyrupcommented, Mar 28, 2022

If it’s of any help Contain(string, OccurrenceConstraint) was added in #1145

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I count the number of matches by a regex?
The MatchCollection instance returned from a call to the Matches method on RegEx will give you the count of the number of matches....
Read more >
How to Count the Number of Matches for a Regex?
In this tutorial, we'll take a look at how to use regular expressions to count the number of matches in some text.
Read more >
Strings
A very extensive set of extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style unit...
Read more >
MatchCollection.Count Property (System.Text. ...
The following example uses the Count property to determine whether the call to the Regex.Matches(String, String) method found any matches.
Read more >
Regex.Count Method (System.Text.RegularExpressions)
Count (String, String, RegexOptions, TimeSpan). Searches an input string for all occurrences of a regular expression and returns the number of matches.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found