Accepted solution by @npinti could be not accurate enough if we take a closer look to the list of ISO 639x codes here. Alternatively you can get a culture list on your own by invoking the static method below (C# code):
System.Globalization.CultureInfo.GetCultures(CultureTypes.AllCultures);
Among the retrieved values, you will find non matching samples as "Cy-az-AZ" (3 codes!), "zh-CHS" (3 letters!) or "en-029" (numbers!).
Curiously enough, the one with numbers does not appear in the MS link above, even though is retrieved by the CultureInfo
method.
This article from here discusses the one with numbers.
So it doesn't seem an easy issue. We could try with a slightly more complex regex as the one shown below, but this doesn't guarantee that we'll be able to distinct an ISO culture code against whatever other thing.
IMO, if we really have the need to be 100% reliable, probably the only choice is to seek that code into the list of codes to find an exact match.
Regex option:
^[^-]{2,3}-[^-]{2,3}(-[^-]{2,3})?$
Find option:
public static bool IsCultureCode(string code)
{
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures); //AllCultures
int i = 0;
while(i < cultures.Length && !cultures[i].Name.Equals(code, StringComparison.InvariantCultureIgnoreCase))
i++;
return i < cultures.Length;
}