正则表达式 - 否定字符串+ OR

时间:2015-06-19 22:38:55

标签: php regex preg-match

我需要匹配不是 11111 22222 (完全)

的字符串
<?php
$string = 'STRING'
$pattern = '#PATTER#' // what I have to find
echo preg_match($pattern, $string) ? 'match' : 'no match';

例:

  

$ string =&#39; 33333&#39 ;; //匹配

     

$ string =&#39; 222222&#39 ;; //匹配

     

$ string =&#39; 11111&#39 ;; //无匹配

     

$ string =&#39; 22222&#39 ;; //无匹配

我尝试了许多我google的模式,但都没有。

注意:必须是纯 REGEX NOT 否定函数 preg_match

2 个答案:

答案 0 :(得分:3)

怎么样:

 ^(?!11111$|22222$).*

测试:https://regex101.com/r/wU7yO0/1

答案 1 :(得分:0)

这个怎么样:

^(|.|..|...|....|(?!11111|22222).....|......+)$

基本思想是:所有长度为0-4和6+的字符串都很好。长度为5的字符串不得为11111或22222。

编辑:并稍微压缩一下(但让IMO的可读性降低):

^(.{0,4}|(?!11111|22222).{5}|.{6}.*)$
相关问题