user3562733
user3562733

Reputation: 35

Regex PHP IF contains and limit

I have this regex and I'm using PHP preg_match to filter the username. I want to create conditions:

This is my code:

/^(?=.{3})[A-Za-z][A-Za-z0-9](?:_[A-Za-z0-9]+)[A-Za-z0-9]$/

I tried a few other ways too. Somehow some valid string are not match. If someone could help me, please also explain to me what is the diff between ?: and ?=

The string I tried:

Upvotes: 3

Views: 104

Answers (1)

xdazz
xdazz

Reputation: 160843

Try the below regex:

if (preg_match('/^[a-z]((?!__)[a-z\d_])+[a-z\d]$/i', $str)) {
  echo 'matched';
}

Upvotes: 2

Related Questions