Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression related: first character alphabet second onwards alphanumeric+some special characters

Tags:

regex

I have one question related with regular expression. In my case, I have to make sure that first letter is alphabet, second onwards it can be any alphanumeric + some special characters.

Regards, Anto

like image 395
anto Avatar asked Sep 11 '10 11:09

anto


People also ask

What is alphanumeric and special characters example?

An alphanumeric example are the characters a, H, 0, 5 and k. These characters are contrasted to non-alphanumeric ones, which are anything other than letters and numbers. Examples of non-alphanumeric numbers include &, $, @, -, %, *, and empty space.

What is the regex for special characters?

Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "."

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.


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

The special characters I have chosen are just an example. Add your own special characters as appropriate for your needs. Note that a few characters need escaping inside a character class otherwise they have a special meaning in the regular expression.

I am assuming that by "alphabet" you mean A-Z. Note that in some other countries there are also other characters that are considered letters.

More information

  • Character Classes
  • Repetition
  • Anchors
like image 151
Mark Byers Avatar answered Sep 29 '22 02:09

Mark Byers


Try this :

 /^[a-zA-Z]/

where

 ^ -> Starts with 
 [a-zA-Z] -> characters to match 
like image 38
DinoMyte Avatar answered Sep 29 '22 02:09

DinoMyte