I have the following regular expression which specifies characters the user is allowed to type:
@"^[A-Za-z0-9/!\$%\^&\*\(\)\-_\+\[\]\{\}\;\:\'\£\@\#\.\?]*$
I know ' \\s
' is the character class for white space, but how do I add this to the regular expression so it excludes it? I have searched for this on Stack Overflow - the questions provide solutions to exclude white space but not how to use it in an existing regular expression. If I add it like I have the other characters, it would mean 'allow white space'?
Edit: Not sure why this has been marked down? Thanks to everyone for their answers
在正则表达式中,方括号中的^
表示取反,因此加^\\s
表示“不是空白”。
First of all, you don't need all those escapes. Second of all, the regex already doesn't allow whitespaces.
@"^[A-Za-z0-9\[\]/!$%^&*()\-_+{};:'£@#.?]*$"
The [] define a set of characters to allow (followed by * means that characters from the characters set can be zero or more times).
^ matches the beginning and $ matches the end, so the fact that /s isn't anywhere there, means white spaces won't be allowed.
In the regular expression, if you don't write "\\s". This means your regular expression doesn't allow any blank spaces.
For example: 1) If I don't want to allow any user to enter blank space in the text box, then the regular expression is written as:
Regex alphabet=new Regex(@"^[az]*$");
2) If the user is allowed to enter any blank space in the text box then the regular expression is written as:
Regex alphabet=new Regex(@"^[az\\s]*$");
3) In regular expression " ^ " means beginning and " $ " means end.
NOTE: ^\\s means negation of "\\s"
Regex regfname = new Regex(@"^[^\\s][a-zA-Z]*$");
this code will not allow any blank spaces and no empty text box!
I hope this will help you to understand the concept of blank spaces in regular expression..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.