i need a regular expression to match only the word’s that match the following conditions. I am using it in my C# program
- Can be any case
- Should not have any numbers
- may contain – and ‘ characters, but are optional
- Should start with a letter
I have tried using the expression ^([a-zA-Z][\'\-]?)+$ but it doesn’t work.
Here are list of few words that are acceptable
- London (Case insensitive)
- Jackson’s
- non-profit
Here are a list of few words that are not acceptable
- 12london (contains a number and is not started by a alphabet)
- -to (does not start with a alphabet)
- to: (contains : character, any special character other that – and ‘ is not allowed)
This matches any word that starts with an alphabetical character, followed by any number of alphabetical characters, – or ‘.
Note that you don’t need to escape the – and ‘ when it’s inside the character [] class, as long as the dash is either the first or last character in the sequence.
Note also that I’ve removed the round brackets from your example – if you don’t want to capture the input, you’ll get better performance by leaving them out.