[英]Regex isMatch for cache pattern key
I wrote test for my cache extension. 我为缓存扩展编写了测试。 I want to delete from cache by pattern.
我想通过模式从缓存中删除。
a) In this example test fails a)在此示例中,测试失败
[Test]
public void Should_found_by_pattern()
{
string pattern = "shouldFoundThis-{0}-{1}-{2}";
var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
var keysToRemove = new List<String>();
var list = new List<string>()
{
"shouldFoundThis-15-2-7"
};
foreach (var item in list)
if (regex.IsMatch(item))
keysToRemove.Add(item);
keysToRemove.Any().ShouldBeTrue();
}
b) In this example test passes b)在此示例中,测试通过
[Test]
public void Should_found_by_pattern()
{
string pattern = "shouldFoundThis-{0}-{1}";
var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
var keysToRemove = new List<String>();
var list = new List<string>()
{
"shouldFoundThis-15-2"
};
foreach (var item in list)
if (regex.IsMatch(item))
keysToRemove.Add(item);
keysToRemove.Any().ShouldBeTrue();
}
Why Regex IsMatch doesn't match pattern "shouldFoundThis-{0}-{1}-{3}" but matches pattern "shouldFoundThis-{0}-{1}" 为什么Regex IsMatch与模式“shouldFoundThis- {0} - {1} - {3}”不匹配,但匹配模式“shouldFoundThis- {0} - {1}”
The regex shouldFoundThis-{0}-{1}-{2}
is identical to the regex shouldFoundThis---
, and there are no three dashes in sequence to be found in your subject string. regex
shouldFoundThis-{0}-{1}-{2}
与正则表达式shouldFoundThis---
相同shouldFoundThis---
并且在主题字符串中没有顺序找到三个破折号。
In constrast, shouldFoundThis-{0}-{1}
is the same as shouldFoundThis-
which can be found in your string. 相比之下,
shouldFoundThis-{0}-{1}
与shouldFoundThis-
相同shouldFoundThis-{0}-{1}
可以在你的字符串中找到。
In a regex, x{n}
means " n
repetitions of x
". 在正则表达式中,
x{n}
表示“ n
次重复x
”。 Read more about quantifiers here . 在这里阅读有关量词的更多信息
You probably meant something like 你可能意味着像
string pattern = @"shouldFoundThis-\d+-\d+-\d+";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.