A regular expression that allows only certain characters in .net

I just worked on some validation and was fixated on that, though :( I want a text containing only [az] [AZ] [0-9] [_].

It must accept any of these characters any number of times in any order. All other characters are marked as invalid.
I tried this, but it does not work!

  {
        ......

        Regex strPattern = new Regex("[0-9]*[A-Z]*[a-z]*[_]*");

        if (!strPattern.IsMatch(val))
        {
            return false;
        }

        return true
  }
+3
source share
2 answers

Do you want to:

Regex strPattern = new Regex("^[0-9A-Za-z_]*$");

Your expression does not work because:

  • , , , . , , , .
  • ^ $. , , . (, "! @# $" ..!). , , , .
  • - . , , * +. (* "0 ", + "1 ".)
+7

:

new Regex("[0-9A-Za-z_]*");
+1

Source: https://habr.com/ru/post/1778675/


All Articles