Regex for string

Posted on 2006-06-06
Medium Priority
Last Modified: 2010-04-16
what is the regex expression for checking that an input only contains a-z (upper or lower) and no other spl-characters/numbers.

Question by:srinivas_vemla
  • 4
  • 2
LVL 25

Expert Comment

ID: 16845320

Author Comment

ID: 16845376
Regex stringPattern = new Regex(@"[a-zA-Z]");

if(stringPattern.Match(input).Success == false)
  errText += "only alphabets pls.";
  valid = false;

This fails when input = "Test 123". What should I change?
LVL 25

Expert Comment

ID: 16845404
It fails because "Test 123" contains a space and numeric characters.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 25

Assisted Solution

dstanley9 earned 200 total points
ID: 16845461
Sorry, I misunderstood.  Use


LVL 64

Accepted Solution

Fernando Soto earned 300 total points
ID: 16845504
Hi srinivas_vemla;

The correct answer to your question is the Regex pattern:

string pattern = "^[a-zA-Z ]+$";    // Will also allow the space character
string pattern = "^[a-zA-Z]+$";    // No space character just a-z upper and lower

The problem with the solution that dstanley9 gave you above is that it will only find the characters a-zA-Z and bypass all others in the string.

The pattern, "^[a-zA-Z]+$", translate to, from the beginning of the string to the end of the string the only characters that are acceptable are a-zA-Z only. The same goes for this pattern, "^[a-zA-Z ]+$", except it also allows the space character.

I hope that this was of some help.

LVL 64

Expert Comment

by:Fernando Soto
ID: 16845532
Hi dstanley9;

This pattern only looks for one character in the string, "^[a-zA-Z]$". Also does not looks for a space.

LVL 25

Expert Comment

ID: 16845572
Thanks, Fernando.  I was assuming that space was included in the "other spl-characters/numbers" requirement, but I forgot the + to match multiple characters.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question