I need to use C# to write a Regular Expression for a title, here is the requirement:
- Title is required (length > 0);
- Maximum 256 characters (length <= 256);
- No character is forbidden, but whitespace only is illegal (the title ONLY containing whitespaces is illegal);
- No leading or trailing whitespaces;
I have already have this:
^.{1,256}$
So how can I meet the rule 3?
EDIT:
- Explained rule 3 more clearly;
- I added rule 4 from Mario’s answer.
I’d skip regular expressions completely, because you can just hardcode string cleanup and validation in two simple steps:
String.Trim(null)to remove all leading/trailing whitespaces.This works, because a name consisting of whitespaces only would be trimmed to 0 length.
Also this avoids using titles such as
" Let's go!".