Click here to Skip to main content
15,850,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've tried to understand how to write a regex that will match for me a pattern with the following characteristics:

1. specific characters - I'm guessing the use of square braces "[]"
2. only one of them (not combination or all) - I'm guessing the use of OR "|"
3. zero or one for the selected character - I'm guessing the use of question mark "?"

I don't know how to combine these in order to get what I want
I tried something like this but it doesn't work

Any help will be great.

good results will be:
bad result:
everything else
Updated 16-Jun-13 6:37am


In a character class (between [ and ] ), only - (the dash) is a special character; ? and | would be treated as 'normal' characters.
Try this:

Explanation about the ^ and $ sign:
the ^ sign mentions the start of the string and the $ mentions the end of the string.

Hope this helps.
Share this answer
Shahare 16-Jun-13 11:44am    
Sorry - checked it and it doesn't do the job - Thanks
Thomas Daniels 16-Jun-13 11:55am    
I updated my answer.
Shahare 16-Jun-13 14:57pm    
Thank you so much - this is what was missing exactly. And mostly thank you for the clear explanation.
Thomas Daniels 18-Jun-13 11:50am    
You're welcome!
Shahare 16-Jun-13 12:29pm    
Thanks - I'll check this later tonight
public static bool IsAlphabet(string strToCheck)
    Regex rg = new Regex("^[a-zA-Z ]*$/;");
    return rg.IsMatch(strToCheck);
Share this answer
Shahare 16-Jun-13 12:29pm    
Thanks - I'll check this later tonight
Balimusi 16-Jun-13 22:24pm    
No problem - hope that helps!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900