用于匹配未通过所有测试场景的单词和数字的正则表达式
Regex to match word and number not passing all test scenarios
使用 .Net Core 3.1,我构建了以下 xUnit 测试来测试我的正则表达式,它应该匹配某些单词,或者那些相同的单词后跟数字:
[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);
}
这是测试 运行 结果的屏幕截图,显示 "Some Company" 和 "Some Company123" 失败:
然而,当我尝试使用 regex101.com 进行相同操作时,它正确地显示输入与正则表达式不匹配,如下两个屏幕截图所示:
为什么这在我的 .NetCore3.1 运行 时间失败了?
@Thefourthbird 回答正确
[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);
}
使用 .Net Core 3.1,我构建了以下 xUnit 测试来测试我的正则表达式,它应该匹配某些单词,或者那些相同的单词后跟数字:
[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);
}
这是测试 运行 结果的屏幕截图,显示 "Some Company" 和 "Some Company123" 失败:
然而,当我尝试使用 regex101.com 进行相同操作时,它正确地显示输入与正则表达式不匹配,如下两个屏幕截图所示:
为什么这在我的 .NetCore3.1 运行 时间失败了?
@Thefourthbird 回答正确
[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);
}