What's the Regular Expression that matches culture names?
I'd like to use a regular expression to filter culture names (like en-US or pt-BR). Anyone has any id开发者_运维问答ea?
Try this:
^[a-z]{2}-[A-Z]{2}$
Or more general (see RFC 4647):
^[A-Za-z]{1,8}(-[A-Za-z0-9]{1,8})*$
If you want follow the RFC 4646 format for the culture name (who is <languagecode2>-<country/regioncode2>
, where <languagecode2>
is the language code and <country/regioncode2>
is the subculture code)
Example: "en", "en-UK", "fr", "fr-FR", ...
Use this Regex:
^[a-z]{2}(-[A-Z]{2})*
C# code sample
Regex.IsMatch(culture, @"^[a-z]{2}(-[A-Z]{2})*$")
Try this to match all available Culture:
^(\w{2,3}-\w{2,4}(-\w{2})?)$
samples:
de-DE
bm-Latn-ML
brx-IN
vai-Vaii-LR
@Gumbo is right. A test:
In [1]: import re
In [2]: reg = re.compile("^[a-z]{2}-[A-Z]{2}$")
In [3]: url = 'en-US'
In [4]: m = reg.match(url)
the result shows that it Matched.
精彩评论