tags:

views:

65

answers:

3

What's wrong with this expression?

^[a-zA-Z]+(([\''\-][a-zA-Z])?[a-zA-Z]*)*$

I want to allow alpha characters with space,-, and ' characters

for example O'neal;Jackson-Peter, Mary Jane

+2  A: 

This will match any string made up of at least one character, which can be alpha characters, hyphen or the single quote mark:

^[a-zA-Z-\']+$

This will also include empty strings:

^[a-zA-Z-\']*$

If it needs to begin and end with alpha characters (as names do):

^[a-zA-Z][a-zA-Z-\']*[a-zA-Z]$
sixfoottallrabbit
+2  A: 

The following is all you need:

^[a-zA-Z' -]+$

The important thing is that the "-" is the last character in the group, otherwise it'd be interpreted as a range (unless you escaped it with "\")

How you actually input that expression as a string in your target language is different depending on the language. For C#, I usually use "@" strings, like so:

var regex = new Regex(@"^[a-zA-Z' -]+$");
Dean Harding
Your regex matches the empty string.
too much php
@too much php: not any more :) though if this is for ASP.NET, you should be using a separate `RequiredFieldValidator` control, since (by default) `RegularExpressionValidator` controls aren't called on empty fields anyway...
Dean Harding
A: 

Something like this?

^[a-zA-Z '\-,]*$
Carko