Im checking some strings with my regex and somehow its not perfect. Im not sure why. I would like to allow string with only these characters:
- A to Z
- 0 to 9
- .
- %
- /
- {space}
- +
- $
So I thought this regex should be enough:
Regex("[^A-Z0-9.$/+%\\- ]$")
But with some string its not really working. I made a small example:
static Regex regex = new Regex("[^A-Z0-9.$/+%\\- ]$");
static void Main()
{
string s;
Console.WriteLine("check: \n");
s = "?~=) 2313";
Console.WriteLine(s + ": " +IsValid(s));
s = "ÄÜÖ";
Console.WriteLine(s + ": " + IsValid(s));
s = "Ü~=) 2313";
Console.WriteLine(s + ": " + IsValid(s));
s = "Ü 2313";
Console.WriteLine(s + ": " + IsValid(s));
s = "~=) 2313 Ü";
Console.WriteLine(s + ": " + IsValid(s));
s = "ÜÜÜ";
Console.WriteLine(s + ": " + IsValid(s));
s = "~=)";
Console.WriteLine(s + ": " + IsValid(s));
s = "THIS--STRING $1234567890$ SHOULD BE VALID.%/ +";
Console.WriteLine(s + ": " + IsValid(s));
Console.ReadKey();
}
public static bool IsValid(string input)
{
if (regex.IsMatch(input)) return false;
return true;
}
As output I get:

The 1.,3. and 4. are True, but this is wrong. What is wrong with my regex? Any ideas? Thank you
It should be
You need to use quantifiers like
+,*to match multiple charactersYour IsValid Class should be