Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

^[A-Za-Z ][A-Za-z0-9 ]* regular expression?

Tags:

c#

regex

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?

like image 710
Surya sasidhar Avatar asked Oct 31 '09 04:10

Surya sasidhar


People also ask

What does AZ ][ A zA z0 9 * mean?

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.

What does the A zA z0 9 +$/ regex check for?

[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.

What is difference [] and () in regex?

[] 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 .

What is a zA 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 ).


1 Answers

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).

like image 171
Peter Di Cecco Avatar answered Sep 26 '22 10:09

Peter Di Cecco