regular expression php,Why does my regular expression not work in PHP?

This question already has answers here:

Match whole string in regex

(2 answers)

Closed 12 days ago.

if (!preg_match("/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,128}/", $password_1)) {

array_push($errors, "Password does not match the requested format");

}

The input for $password_1 is more than 128 characters, but it still results in true. But this expression works with the tag in HTML i.e.

But my doubt is: Why doesn't it work in PHP?

I could try not to use regex, and use manual if statements in php, but it feels like using this regex.

INFORMATION:

I am not good at regex. Please help me with detail.

AND:

I think that regex should match anything that is:

Text more than 6 characters, and less than 128 characters including spaces and stuff

A-Z and a-z and digits.

Special characters

I'm pretty sure there is something wrong with the whole regex. Please help!!

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

Within your regex you are missing the start- and end-boundaries (^ and $). Your current regex will just match the first 128 characters that matches your requirements and ignores the rest: https://regex101.com/r/ZLmRHq/1

However if you add the boundaries, you get

/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,128}$/

so the match should start at the beginning of the string, and end at the end of the string: https://regex101.com/r/ZLmRHq/3/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值