1

I need to verify input which is like en-US or en-us or ar-AE I have searched net and found it bit difficult to understand and create one regular expressing on string which is no more than 5 characters in length and should be case in-sensitive.

I created one [a-z][a-z][-][a-z][a-z] this one works fine but it doesn't check the length it will match en-USXYZ also

Regards

1
  • 3
    If you are matching cultures, be aware that not all are two two-letter parts. There are some that has. For instance, some chinese cultures deviate from this (such as zh-Hans- Chinese (Simplified)). Also, some cultures has three parts, such as sr-Cyrl-BA - Serbian, Cyrillic alphabet (Bosnia and Herzegovina). Commented Jan 23, 2012 at 10:19

7 Answers 7

4

This is what anchors are for:

(?i)^[a-z]{2}-[a-z]{2}$

The case-insensitive option (?i) can also be set when compiling the regex:

Regex regexObj = new Regex("^[a-z]{2}-[a-z]{2}$", RegexOptions.IgnoreCase);
Sign up to request clarification or add additional context in comments.

Comments

4

Use such one:

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

Comments

3

Try this:

^[a-z][a-z][-][a-z][a-z]$

Comments

1

Use this regular expression:

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

Comments

0

You use ^ and $ to specify the start and end of the string.

^[a-z][a-z]-[a-z][a-z]$

Or using multiplier:

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

You would also have to include the A-Z interval for upper case characters, unless you have specified case insensetivity for the Regex object:

^[A-Za-z]{2}-[A-Za-z]{2}$

Comments

0

You should use anchors in your expression:

^[a-z][a-z][-][a-z][a-z]$

Comments

0

\w{2}[-]\w{2} should do the trick:

\w -> character
{2} -> exact 2 times

EDIT: \w also allows number so [a-z]{2}[-][a-zA-Z]{2}

1 Comment

It also matches en-USassd while it should fail for such condition it doesnt check for length of string

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.