Himanshu Agrawal
Himanshu Agrawal

Reputation: 129

Php check a specific pattern

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

Answers (1)

The fourth bird
The fourth bird

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

Regex101 demo | Php demo

Upvotes: 1

Related Questions