Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex IsMatch all Letters except 'X', for example

How to do something like this with Regex? :

string myString = "smth there";
foreach (char c in myString)
{
    if (char.IsLetter(c) && !c.Equals('X')) return false;
}

I have tried this:

if (Regex.IsMatch(number, @"[A-Za-z^X]")) return false;

Because [^X] is "All except X" and as expected – nothing worked, as always.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can’t negate only a portion of a character class using a caret. You must either negate the entire class (by having ^ as the first character in the class definition), or none of it. You could use the regex [A-WYZ] with the IgnoreCase option. That character class matches only the characters A-W, and Y and Z.

Or, you could use character class subtraction. The regex [A-Z-[X]] will match all characters from A to Z, except X.

To match the full string instead of looping over characters, you could do ^[A-Z-[X]]+$. Outside of [], a caret (^) matches the start of a string, and a $ matches the end of a string (or line if the Multiline option is specified)

Note that this won’t match your string myString = "smth there";, because you’ve excluded spaces.

Regex.IsMatch("smththere", @"^[A-Z-[X]]+$", RegexOptions.IgnoreCase); // true

Regex.IsMatch("smth there", @"^[A-Z-[X]]+$", RegexOptions.IgnoreCase); // false (because of the space)

Regex.IsMatch("xinthisstring", @"^[A-Z-[X]]+$", RegexOptions.IgnoreCase); // false because x

Alexei makes a very good point below:

Character class is easy – @"^[\w-[X]]+$", it is A-Z that is tricky

You can exclude the character 'X' from your character class with -[X] in the class, but replicating the behavior of char.isLetter is tricky, since char.isLetter is true for all unicode letters in addition to ASCII letters. You could explicitly specify that your character class contains all unicode letters that char.isLetter is true for, but that would be tedious.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading