-3

I am trying to check if a string contain at least, a min, a maj and a number.

m2A is Ok

m2a is not Ok.

But when I try this:

$test = "m2A";
$regex = '^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)$';
preg_match($regex,$test,$matches);
var_dump($matches);

I've got an error

preg_match(): No ending delimiter '^' found

I can't see what is wrong with this

Minirock
  • 628
  • 3
  • 13
  • 28

2 Answers2

1

There are two problems with your current PHP script. First, your regex string needs to be surrounded by delimiters, so use something like this:

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).*$/

Notice also the second fix, which is that I add .* to your pattern, to consume anything, assuming that all three of your lookaheads have succeeded.

$test = "m2A";
$regex = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).*$/';
preg_match($regex,$test,$matches);
var_dump($matches);

array(1) {
  [0]=>
  string(3) "m2A"
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks actually I already tried to add the '/' but I was getting an empty array. It's because I missed the star at the end actually thanks. – Minirock Feb 20 '19 at 14:44
1

PHP regex needs delimiters. try adding '/' on both sides like so

$regex = '/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)$/';
guygrinberger
  • 327
  • 2
  • 14