I need to use C# to write a Regular Expression for tags, here is the requirement:
- Tags are optional;
- Maximum 9 tags;
- no duplicated tags;
- Maximum 30 characters for each tag;
- Each tag should only contain reasonable characters (how to define?, I’m now using
[\w-]); - Tags are separated by
,(with or without one whitespace after the comma are optional); - The ending
,(with or without one whitespace) is also legal.
I have already had this:
^(|[\w-]{1,30}(,\s?[\w-]{1,30}){0,8}(,\s?)?)$
I find it’s difficult to meet all the rules. especially rule 3.
EDIT:
- Updated rule 5, rule 6;
- Applied rule 7.
Do it without Regex: