3

I have a simple query. I am new to regular expression validations in C#.

I want some expression which would allow only single alphabet that means a-z or A-Z. It should not allow whole word. Can anyone suggest me an expression?

1 Answer 1

5

The following should work:

[a-zA-Z]+

This expression allows one or more characters in the specified range. And if you want only a single character:

[a-zA-Z]
Sign up to request clarification or add additional context in comments.

5 Comments

Please realize that these expressions also matches the comma as it is included between the square brackets…
@mousio, thanks for pointing this out, I've updated my answer to fix this.
@mousio sorry bt i did not get your point. did you mean that it will allow that is accept the comma alongwith the single character???????
A "character class" (or "character set") matches only one out of several characters, just place the characters you want to match in square brackets. A character class matches only a single character. gr[ae]y will not match graay, graey or any such thing. Use a hyphen to specify a range: [0-9] matches a single digit between 0 and 9. You can use more than one range: [a-zA-Z] matches a single letter, case insensitively. You can combine ranges and single characters. [a-z,A-Z] matches a lowercase or uppercase letter or the comma. The order of characters and ranges does not matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.