Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

c# - How can I modify this regular expression to not allow white spaces?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...