Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find NOT matching characters in a string with regex?

Tags:

c#

.net

regex

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?

like image 235
silla Avatar asked Sep 12 '12 08:09

silla


2 Answers

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(); 
like image 168
Habib Avatar answered Sep 21 '22 04:09

Habib


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");
like image 37
JNF Avatar answered Sep 24 '22 04:09

JNF