Reputation: 129
I want to check a certain pattern in a string. My pattern contains 3 parts:
I am using following regex:
preg_match('/^[a-zA-Z0-9]+[@\-]*[a-zA-Z0-9]+$/i');
and tested on string :
a-121kabrastreet@90
which evaluates to false. Please rectify my logic. Thank you
Upvotes: 0
Views: 50
Reputation: 163362
The alphabets and digits should also be added to the character class in the middle. Note that you don't have to escape the hyphen if it is places at the end of the character class.
^[a-zA-Z0-9][@a-zA-Z0-9-]*[a-zA-Z0-9]$
^^^^^^^^^
You use the case insensitive flag, your pattern could look like:
/^[a-z0-9][@a-z0-9-]*[a-z0-9]$/i
Upvotes: 1