供您参考,我放弃了检查重复标签的正则表达式(规则 3)。我仍在将正则表达式用于其他规则。
public static bool CheckTags(string tags)
{
if (!Regex.IsMatch(tags, @"^(|[\w-]{1,30}(,\s?[\w-]{1,30}){0,8}(,\s?)?)$"))
return false;
// Step 1: Check duplicated tags
// Step 1.1: Remove the whitespace first
tags = Regex.Replace(tags, @"\s+", @"");
string[] tagsArray = tags.Split(',');
// Step 1.2: Check if duplicated items exist
if (tagsArray.Distinct().Count() != tagsArray.Length)
return false;
return true;
}
通过了以下测试用例:
[TestMethod]
public void CheckTagsTest()
{
// Whitespaces and commas only
Assert.IsFalse(CheckTags(" \t \n \u1680"));
Assert.IsFalse(CheckTags(" , , , "));
Assert.IsFalse(CheckTags(",,,"));
Assert.IsFalse(CheckTags(",, ,"));
// 10 tags
Assert.IsFalse(CheckTags(@"tag0,tag1,tag2,tag3,tag4,tag5,tag6,tag7,tag8,tag9"));
// Duplicated tags
Assert.IsFalse(CheckTags(@"tag0,tag0"));
// A tag contains more than 30 characters
Assert.IsFalse(CheckTags(@"abcdefghijklmnopqrstuvwxyz0123*"));
// A tag contains invalid characters
Assert.IsFalse(CheckTags(@"tag!"));
Assert.IsFalse(CheckTags(@"tag*"));
// Tag separator contains more than one whitespaces
Assert.IsFalse(CheckTags(@"tag1, tag2, tag3"));
// Normal tags
Assert.IsTrue(CheckTags(@"tag1, tag2, tag3"));
Assert.IsTrue(CheckTags(@"tag1,tag2, tag3"));
Assert.IsTrue(CheckTags("tag1,tag2,\ttag3, tag4"));
// Ending tag separator is allowed
Assert.IsTrue(CheckTags("tag1,tag2,\ttag3, tag4,"));
Assert.IsTrue(CheckTags("tag1,tag2,\ttag3, tag4, "));
// A tag contains 30 characters
Assert.IsTrue(CheckTags(@"abcdefghijklmnopqrstuvwxyz0123"));
// A tag contains '-'
Assert.IsTrue(CheckTags(@"T-shirt"));
Assert.IsTrue(CheckTags(@"-"));
// A tag contains '_'
Assert.IsTrue(CheckTags(@"T_shirt"));
Assert.IsTrue(CheckTags(@"_"));
// A tag contains chinese characters
Assert.IsTrue(CheckTags("\u4E2D"));
}
功劳仍归 Cedric Bignon。