tags:

views:

477

answers:

5

this expression describe "first letter should be alphabet and remaining letter may be alpha numerical". But i want it should allow special characters also like, when i enter "C#" it is raising error. i want to enter special character also and first letter should alphabet". help me thank you.

A: 

Try this:

^[A-Za-z ].*

joemoe
Did you mean `^[A-Za-z].*` ?
Zarel
It is not taking as expression Mr. Joemoe
Surya sasidhar
ya it is not accepting in property window
Surya sasidhar
A: 
^[A-Za-z](\W|\w)*

(\W|\w) will ensure that every subsequent letter is word(\w) or non word(\W)

instead of (\W|\w)* you can also use .* where . means absolutely anything just like (\w|\W)

Xinus
Thank u it is working Mr. Xinus
Surya sasidhar
How is this different from "^[A-Za-z].*" or even "^[A-Za-z]"?
paxdiablo
@Xinus: the character class [\W|\w] means word characters, the | character, or non-word characters. If you changed your square braces to parenthesis, it would mean what you claimed. But it could still be more simply and more elegantly expressed.
Asaph
@Asaph: I do not have much experience with regular expressions ... I think you are saying sounds logical, I am changing it to parenthesis ...
Xinus
@paxdiablo: "^[A-Za-z]" would only match the first character, not the whole word. "^[A-Za-z].*" will work fine though, as Xinus also stated after his edit.
Marc Müller
@Xinus This answer is likely not correct. The initial regular expression matches a valid C identifier. This solution will, unfortunately match "FOO\n\n\n" as well as "C#".
pst
`(\W|\w)` is *not* the same as `.` since `\W` will always include newlines, whilst for `.` it depends on the mode.
Peter Boughton
@Marc, if you're using the start anchor (^), I'm assuming (perhaps wrongly) that a missing end anchor ($) means just match at the start of the string. That's why I asked what the difference was. If it matches the whole string, then the start anchor would be superfluous.
paxdiablo
+1  A: 

This expression will force the first letter to be alphabetic and the remaining characters to be alphanumeric or any of the following special characters: @,#,%,&,*

^[A-Za-z][A-Za-z0-9@#%&*]*$
Asaph
Surya sasidhar
Asaph
@Surya sasidhar: Do you want to allow spaces? Your original regex in the title appears to allow spaces.
Asaph
you should escape *
Xinus
@Xinus: No. * doesn't need to be escaped when it's within a character class.
Asaph
+1  A: 

How about

^[A-Za-z]\S*

a letter followed by 0 or more non-space characters (will include all special symbols).

Mark
+1  A: 
Peter Di Cecco
@Peter Di Cecco: You forgot 0-9.
Asaph
Good call. I'll fix it.
Peter Di Cecco