Regular expression for characters

In C #, I use a RegexValidator to test a field that can only contain values ​​of L, l, M, m, D, d. I tried using [RegexValidator ("[l | L] [M | m] [D | d]" ... but this will not work. Any ideas?

thank

+3
source share
5 answers

This is a regex:

[l|L][M|m][D|d]

means:

  • l, | or L; then
  • M, | or m; then
  • D, | or d.

Try:

^[LMD]+$

as a case insensitive match if you can do this or:

^[LlMmDd]+$

if you can not.

, L, l, M, m, D d. + , , * 0 , .

Edit: , , , , :

^[LlMmDd]$
+4

. , .

/[^LlMmDd]+/
+1

, -

var s1 = "Ll";
var s2 = "m";
var s3 = "LmD";
var pattern = "^[LMD]$";

Console.WriteLine( Regex.IsMatch(s1, pattern, RegexOptions.IgnoreCase) );
Console.WriteLine( Regex.IsMatch(s2, pattern, RegexOptions.IgnoreCase) );
Console.WriteLine( Regex.IsMatch(s3, pattern, RegexOptions.IgnoreCase) );

False

False

Regex , char

char c = 'd';
Console.WriteLine( Regex.IsMatch(new String(c,1), pattern, RegexOptions.IgnoreCase));
0

regexp. Google ( Bing) regexp.

[asdf] char, a, s, d, f.
[asdf]* , a, s, d f.
[asdf]+ , a, s, d f.

Regexp | OR [] .

[A-Z][a-z]+[0-9] , ( char -), , , ( char a z) .

[l|L][M|m][D|d] , 3 .
char l, |, l.
char M, |, M.
char d, |, d.

[lL][Mm][Dd]may be what you want to use.
[lLMmDd]+to line at least one char, both limited l, l, M, M, dor d.
[lLMmDd][lLMmDd][lLMmDd]+for row length of at least three symbols, wherein all symbols are limited either l, l, M, M, dor d.

0
source

This code should read the line from the console and match it. It must accept only 1 character and matches the characters you specify.

     static void Main(string[] args)
    {
        Regex regex = new Regex(@"^[L|M|D]$", RegexOptions.IgnoreCase);
        System.Console.WriteLine("Enter Text");
        String str = System.Console.ReadLine();
        Match match = regex.Match(str);

        if (match.Success == true)
        {
            System.Console.WriteLine("Success");
        }
        else
        {
            System.Console.WriteLine("Fail");
        }
        System.Console.ReadLine();
    }
0
source

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


All Articles