tags:

views:

136

answers:

7

Hi all ..

I'm quite new to Regex'es, so I was wondering if this is possible?

Take a string and check that all characters are A-Z or a-z.

My best guess so far is:

"^[A-Za-z]*$"

But it seems to have some trouble if a character in the middle of the string is not a valid character (fx. "aaa__aa"). The Regex.IsMatch returns true.. :(

[EDIT] A few comments on your answer rather than only a regex would be great :)

+2  A: 

Try this

"^[A-Za-z]*$"
Martin Liversage
Sorry xD That's what I meant. Wrong copy-paste :)
cwap
This will match the empty string since `^[A-Za-z]*$` says match any of A-Z or a-z _zero_ or more times. Zero allows for an empty line. @Meeh: Is that what you want?
Telemachus
+1  A: 

I guess you want

"^[A-Za-z]+$"
SilentGhost
+1  A: 

What you are currely saying is
The string is only valid if it contains the characters between 0 - 9 or nothing

To check if the characters are a-z or A-Z you need to used

^[A-Za-z]*$

Which says
The string is only valid if the characters are between a - z or A - Z (inclusive) or nothing

EDIT
The new regex you have edited the previous one to works fine, and doesn't match anything with a underscore in. You may have an issue in your code implementation.

Yacoby
Ouch, yeah, I had an error :( Thanks..
cwap
Unless this variety of regular expressions is different from what I'm familiar with, this actually says "The string is only valid if the characters are between a-z or A-Z _or it's a blank line_." The star means _zero_ or more times. A blank line would satisfy the zero requirement.
Telemachus
+1  A: 

Yes, this should work. You alternatively like to use something like:

^[A-Za-z]+$

to ensure that there is at least one alphabetic character. What language are you using? In python, this would be better achieved with the isalpha() function. There may be similar functions in your chosen language. Also, in a lot of implementations of regular expressions, you can simply do:

^\a+$

In some regular expression implementations (POSIX in particular), the + needs to be \+.

Al
+1  A: 

^[a-zA-Z]+$ should do the job. But when you want to catch sentence with spaces, ^[a-zA-Z\s]+$ will be better, where \s means space for this one. Or you may even extending it to catch punctuations character also.

antreality
A: 

"aaa_aaa" is still a match because it matches the first block of a's. By incorporating the '^' and'$' into your expression, you are indicating that the start of linemust first match, followed by a block of letters, followed by the end of line. This essentially means the whole string must match the expression.

SP
A: 
using System;
using System.Text.RegularExpressions;

public class Test
{
  public static void Main() {
    if (Regex.IsMatch("aaa_aa", "^[a-zA-Z]*$"))
      Console.WriteLine("Pigs can fly");
  }
}
p00ya
Just to demonstrate that the earliest answer WFM.
p00ya
Doesn't this still have the problem that it matches an empty line?
Telemachus
all characters in the empty string are alphabetical. There was no requirement that empty strings shouldn't be permitted.
p00ya
@p00ya: The OP is trying to validate a string, and he said "Take a string and check that all characters are A-Z or a-z." You may be right, but I seriously doubt that he wants to pass on a blank line.
Telemachus