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

How to invert a regular expression?

There is a piece of code. It allows you to enter only oriental characters (Japanese, Chinese, and the like) into the TextBox. But how to do the opposite? Allow to enter everything EXCEPT for these characters?

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (Regex.IsMatch(textBox1.Text, "[^\u2E80-\u9FFF]"))
    {
        textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
        textBox1.SelectionStart = textBox1.TextLength;
    }
}

>Solution :

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

To allow entering everything except Oriental characters (Japanese, Chinese, etc.), you can modify the regular expression to match any character that is not in the specified Unicode range. You can do this by changing the regex pattern from "[^\u2E80-\u9FFF]" to "[\u0000-\u2E7F\u9FFF-\uFFFF]".

Here is the modified code that allows entering everything except Oriental characters:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (Regex.IsMatch(textBox1.Text, "[\u0000-\u2E7F\u9FFF-\uFFFF]"))
    {
        textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
        textBox1.SelectionStart = textBox1.TextLength;
    }
}

This code uses a negated character class "[^\u2E80-\u9FFF]" to match any character that is not in the specified Unicode range. The range is now inverted to [\u0000-\u2E7F\u9FFF-\uFFFF] to match any character that is not in the Oriental character range. If the input string contains any Oriental characters, the code removes the last character from the TextBox and sets the cursor to the end of the remaining text.

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