Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression (first character matching a-z)

Tags:

regex

I have this regex: /[^a-zA-Z0-9_-]/

What I want to add to above is:

  • first character can be only a-zA-Z

How I could make this regular expression?

like image 838
Nima Nm Avatar asked Dec 11 '11 07:12

Nima Nm


People also ask

How do I find the first character in a regular expression?

The regular expression to match String which contains a digit as first character is “^[0-9]. *$”.

What does AZ do in regex?

Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter. In a character set a ^ character negates the following characters.

What does Z mean in regex?

\Z is same as $ , it matches the end of the string, the end of the string can be followed by a line break. \z matches the end of the string, can't be followed by line break.

How do I match a specific character in regex?

Match any specific character in a setUse square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.


1 Answers

Try something like this:

^[a-zA-Z][a-zA-Z0-9.,$;]+$ 

Explanation:

^                Start of line/string. [a-zA-Z]         Character is in a-z or A-Z. [a-zA-Z0-9.,$;]  Alphanumeric or `.` or `,` or `$` or `;`. +                One or more of the previous token (change to * for zero or more). $                End of line/string. 
like image 187
dinesh Avatar answered Sep 18 '22 22:09

dinesh