regular expression to detect ISO language code

c#, regex, winforms

Solution

Something like so should work: `^[a-z]{2}-[A-Z]{2}$`.

The `^` anchor instructs the regex engine to start matching from the beginning of the string, `[a-z]` means any lower case letter between `a` and `z`. `{2}` means exactly 2 repetitions of. The same explanation holds for the rest. Finally, the `$` instructs the regex engine to stop matching at the end of the string.

Problem

I'm trying to detect whether a combo box contains an ISO language code (i.e. en-GB, el-GR, ru-RU etc), which comprises of 2 alphabetical characters, a dash, and 2 more alphabetical characters (in upper case, or it might not matter?). I was wondering, is there a way I can achieve this using regular expressions? I'm assuming the expression would look something like this (but I don't have much experience in the subject): ``` string pattern = @"^\a{2,2}-\a{2,2}"; ```

Original source

Related problems