Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Regular Expression that matches culture names?

Tags:

regex

I'd like to use a regular expression to filter culture names (like en-US or pt-BR). Anyone has any idea?

like image 343
Rodrigo Waltenberg Avatar asked Sep 09 '10 12:09

Rodrigo Waltenberg


2 Answers

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})*$
like image 108
Gumbo Avatar answered Oct 19 '22 13:10

Gumbo


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})*$")
like image 45
rdhainaut Avatar answered Oct 19 '22 11:10

rdhainaut