dragon87836215 2016-05-11 13:36
浏览 18
已采纳

preg_match多重模式

Previous questions explain some issues like this, but I couldn't implement solution for my issue.

I tried to create pattern for: 1. Big Character 2. Small Character 3. Number 4. SpecialChar

All this conditions must contain my string. I can check conditions 1,2,3 with:

[0-9A-Za-z]

I can check condition number 4 (special chars) with:

[[:punct:]]

But I can't get this combination of them to work:

$p = "aAbB4#"; //correct string

if(!preg_match('/([0-9A-Za-z]{4,50}|[[:punct:]])/',$p)){
      $p = "Not all credentinals are correct";
}

How could I do that?

  • 写回答

1条回答 默认 最新

  • drny60365 2016-05-11 13:57
    关注

    Use positive look-ahead matching for this, i.e. put every character into (?=) group:

    if (!preg_match('/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[[:punct:]]).{4,50}$/', $p)) {
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog