If Im able to check a string if there are invalid characters:
Regex r = new Regex("[^A-Z]$");
string myString = "SOMEString"; 
if (r.IsMatch(myString)) 
{     
  Console.WriteLine("invalid string!");
} 
it is fine. But what I would like to print out every invalid character in this string? Like in the example SOMEString => invalid chars are t,r,i,n,g. Any ideas?
Use LINQ. Following will give you an array of 5 elements, not matching to the regex.
char[] myCharacterArray = myString.Where(c => r.IsMatch(c.ToString())).ToArray();
foreach (char c in myCharacterArray)
{
    Console.WriteLine(c);
}
Output will be:
t
r
i
n
g
EDIT:
It looks like, you want to treat all lower case characters as invalid string. You may try:
   char[] myCharacterArray2 = myString
                                   .Where(c => ((int)c) >= 97 && ((int)c) <= 122)
                                   .ToArray(); 
                        In your example the regex would succeed on one character since it's looking for the last character if it isn't uppercase, and your string has such a character.
The regex should be changed to Regex r = new Regex("[^A-Z]");.
(updated following @Chris's comments)
However, for your purpose the regex is actually what you want - just use Matches.
e.g.:
foreach (Match item in r.Matches(myString))
{
   Console.WriteLine(item.ToString() + " is invalid");
}
Or, if you want one line:
foreach (Match item in r.Matches(myString))
{
   str += item.ToString() + ", ";
}
Console.WriteLine(str + " are invalid");
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With