Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using regex to validate input formatting in C#

This is a super basic question (I am brain dead today):

How do I validate in input using regexes, to see: 1) if the input is in a certain form 2) if the input is all caps (just casting the input to caps is not feasible for this)

I want ot make sure my inputs are in the form XX_XX. Here isi what I have:

public bool IsKosher(string input)
{
    Regex r = new Regex(input);
    if(r.Matches([A-Z]_[A-Z]))
    {
        return true;
    }
    return false;     
}

Any ideas why it's not compiling?

Thank you!

like image 272
gfppaste Avatar asked Mar 17 '26 07:03

gfppaste


1 Answers

You are missing double quotes, you put parameters in wrong places, and you do not need an if statement:

public bool IsKosher(string input) {
    return Regex.IsMatch(input, "[A-Z]{2}_[A-Z]{2}");
}
like image 196
Sergey Kalinichenko Avatar answered Mar 19 '26 19:03

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!