The regular expression ^[A-Za-Z ][A-Za-z0-9 ]*
describe "first letter should be alphabet and remaining letter may be alpha numerical". But how do I also allow special characters? When I enter "C#" it is raising an error.
How do I enter a special character and first letter should alphabet?
The bracketed characters [a-zA-Z0-9] mean that any letter (regardless of case) or digit will match. The * (asterisk) following the brackets indicates that the bracketed characters occur 0 or more times.
[a-zA-Z0-9. _-:\?] means it can be among the all the Uppercase and lowercase letters and the number betwween 0 and 9, and the letter.
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9. (a-z0-9) -- Explicit capture of a-z0-9 .
For example [a-zA-Z0-9]+ is a pattern that matches against a string of any length, as long as the string contains only lowercase letters ( a-z ), uppercase letters ( A-Z ), or numerals ( 0-9 ).
A lot of the answers given so far are pretty good, but you must clearly define what it is exactly that you want.
If you would like a alphabetical character followed by any number of non-white-space characters (note that it would also include numbers!) then you should use this:
^[A-Za-z]\S*$
If you would like to include only alpha-numeric characters and certain symbols, then use this:
^[A-Za-z][A-Za-z0-9!@#$%^&*]*$
Your original question looks like you are trying to include the space character as well, so you probably want something like this:
^[A-Za-z ][A-Za-z0-9!@#$%^&* ]*$
And that is my final answer!
I suggest taking some time to learn more about regular expressions. They are the greatest thing since sliced bread!
Try this syntax reference page (that site in general is very good).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With