Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi guys, i need to make a pattern, this is my first program where i use this regex thing and I'am not much familiar with it, basically i need to allow the string to have space ot better " ", but if i put that in the pattern, for the regex, that means, it must have a whitespace, so how to make, for regex to dont care if there is a whitespace or not?
and i also need to allow "/" and "\",

so how to allow " ", "/" and "\" in the string for the regex basically to skip them?
i hope you understand me. for more info ask me. tnx in advance.

btw \s means must have whitespace :/ which is not useful for me.
Posted

Depends on what you are trying to do - and it isn't clear from your description.
To match your "whitespace" collection is easy:
[\s\\/]
will match any character which is any one of them, and
[^\s\\/]
will match any character which is none of them.

But there isn't a code which says "ignore these characters, regardless of where they are in the string" - for that kind of thing, you are better off using string.Replace in C# to remove them before passing the data to a Regex for matching.

And if you are going to work with regexes, then get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions. I use it a lot!
 
Share this answer
 
Try this:
C#
string input = "This is an input string with spaces.";
if (Regex.IsMatch(input, @"[ /\\]"))
{
    // string contains a space, a slash, or a backslash or a mix of them
}
else
{
    // string does not contain any of those chars
}
 
Share this answer
 
Comments
SrgjanX 9-Jan-15 11:34am    
thank both of you, replaced the pattern and works now :) tnx for the quick answers.
Thomas Daniels 9-Jan-15 11:36am    
You're welcome!
The others have already answered your specific question, but I wanted to point out a couple sites that will help you to learn RegEx.
A very helpful site is regexlib[^]; they have a large library of Regular Expressions. They also have a RegEx tester[^], which is very useful when trying to see what your reg ex actually does. Also a very useful RegEx tool is available here[^].
 
Share this answer
 
Comments
SrgjanX 14-Jan-15 20:20pm    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900