What is a regular expression that matches crop names?

I would like to use regex to filter crop names (e.g. en-US or pt-BR). Does anyone have an idea?

+5
source share
3 answers

Try the following:

^[a-z]{2}-[A-Z]{2}$

Or more general (see RFC 4647 ):

^[A-Za-z]{1,8}(-[A-Za-z0-9]{1,8})*$
+11
source

If you want to follow the RFC 4646 format for the name of the culture (who <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 example

Regex.IsMatch(culture, @"^[a-z]{2}(-[A-Z]{2})*$")
+1
source

@Gumbo . :

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)

, .

0
source

All Articles