views:

69

answers:

2

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

+6  A: 

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})*$
Gumbo
Thanks! Unfortunately my ASP.NET MVC2 route doesn't work anyway :/
Rodrigo Waltenberg
@Rodrigo Waltenberg: Note that `^` and `$` mark the begin and end of the string respectively. Maybe you need that regular expression without these anchors.
Gumbo
A: 

@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.

Tauquir
Please refrain from posting non answers like this. If you have an answer, post it. If you are commenting on an answer, post a comment on that answer.
Oded
Humble apologies. I will take care in future. Thanks. :)
Tauquir