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
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
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]$
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' -]+$");