lokendra jayaswal
lokendra jayaswal

Reputation: 308

Match First Regex Character

Is there any regular expression to match first character of string to be either Underscore(_) or alphabets[a-zA-Z] the second character onward string can contain [0-9a-zA-Z] or only 2 special characters that are '_' and '-'

Upvotes: 0

Views: 3472

Answers (2)

Chirag Bhatia - chirag64
Chirag Bhatia - chirag64

Reputation: 4526

^[_a-zA-Z][\w-]*$

This should check for the first character for _, a-z & A-Z and from then onwards, alphanumeric, _ and '-'.

Edit: Replaced alphabetic with alphanumeric

Edit2: Removed unnecessary escape characters

Upvotes: 2

Explosion Pills
Explosion Pills

Reputation: 191779

^[_a-zA-Z][\w-]+$

This is a character class with _, and the alphabet characters. \w is alphanumeric plus _, so the cash is included in the character class. + means "one or more."

Upvotes: 2

Related Questions