我想验证标签列表 - 用空格分隔的字符串。例子:
"blue white green123 #$#! ()!!!123 q_w_e_r_t_y"
唯一的要求是他们不能以下划线'_'开头。什么是匹配这些标签的合适的正则表达式?
我写了一些测试用例来验证模式的正确性:
public void RegexTest()
{
//arrange
const string pattern = @"^PATTERN$";
var regex = new Regex(pattern);
var positive = new[] { "AAA", "A_", "AAA AAA", "AAA_AAAA", "AAA_AA AAA_aaa AA___ AAA", "A____", "333A%#$%#@%$__=-21-2-AA213", "+=-_0987654321`!@#$%^&*() qwertyu:/.," };
var negative = new[] { "_AAAA", "A _AA ", "AA _AA", "A B _C", "_ " };
//act
var positiveMatches = positive.Select(x => regex.IsMatch(x)).ToArray();
var negativeMatches = negative.Select(x => regex.IsMatch(x)).ToArray();
//assert
CollectionAssert.AreEqual(positiveMatches.Select(x => true).ToArray(), positiveMatches);
CollectionAssert.AreEqual(new bool[negativeMatches.Length], negativeMatches);
}
答案 0 :(得分:2)
答案 1 :(得分:1)
答案 2 :(得分:1)