Regex to match word and number not passing all test scenarios

Viewed 103

Using .Net Core 3.1, I have built the following xUnit test to test my regex that is supposed to match certain words, or those same words followed by numbers:

[Theory]
    [InlineData("Some Demo543 Company", true)]
    [InlineData("Some Company", false)] //why is this one failing
    [InlineData("Some Company123", false)] //why is this one failing
    [InlineData("Some Test123 Company", true)]
    [InlineData("Some Testing123 Company", true)]
    [InlineData("Some Example123 Company", true)]
    [InlineData("Some Demonstration321 Company", true)]
    [InlineData("demo123", true)]
    [InlineData("Testing123", true)]
    [InlineData("Company123", false)]
    [InlineData("Company", false)]
    public void TestRegexSentenceContainsWholeWordWithNumber(string sentence, bool expectedResult)
    {
        string pattern = @"\b[example|demo|demonstration|test|testing]+\d*\b";
        var re = new Regex(pattern, RegexOptions.IgnoreCase);
        re.Match(sentence).Success.ShouldBe(expectedResult);
    }

Here is a screenshot of the test run result, showing that "Some Company" and "Some Company123" are failing:

test run result

Yet, when I try the same using regex101.com, then it correctly shows that the input does NOT match the regex, as shown in the next two screen shots: also working on regex101 also working on regex101

Why is this failing in my .NetCore3.1 runtime?

1 Answers

@Thefourthbird correct answered.

[Theory]
[InlineData("Some Demo543 Company", true)]
[InlineData("Some Company", false)] 
[InlineData("Some Company123", false)] 
[InlineData("Some Test123 Company", true)]
[InlineData("Some Testing123 Company", true)]
[InlineData("Some Example123 Company", true)]
[InlineData("Some Demonstration321 Company", true)]
[InlineData("demo123", true)]
[InlineData("Testing123", true)]
[InlineData("Company123", false)]
[InlineData("Company", false)]
public void TestRegexSentenceContainsWholeWordWithNumber(string sentence, bool expectedResult)
{
    const string pattern = @"\b(?:example|demo|demonstration|test|testing)\d*\b";
    var re = new Regex(pattern, RegexOptions.IgnoreCase);
    Assert.Equal(expectedResult,  re.Match(sentence).Success);
}

enter image description here

Related