正则表达式 - 否定字符串 + OR
Regular Expression - Negation String + OR
我需要匹配不是 11111 或 22222 (EXACT)
的字符串
<?php
$string = 'STRING'
$pattern = '#PATTER#' // what I have to find
echo preg_match($pattern, $string) ? 'match' : 'no match';
案例:
$string = '33333';// match
$string = '222222';// match
$string = '11111';// no match
$string = '22222';// no match
我尝试了很多我 google 和 none 有效的模式。
NOTE: 它必须是纯 REGEX 和 NOT 取反函数 preg_match
这个怎么样:
^(|.|..|...|....|(?!11111|22222).....|......+)$
基本思想是:所有长度为0-4和6+的字符串都可以。长度为5的字符串一定不能是11111或22222.
编辑:并稍微压缩长度(但降低 IMO 的可读性):
^(.{0,4}|(?!11111|22222).{5}|.{6}.*)$
怎么样:
^(?!11111$|22222$).*
我需要匹配不是 11111 或 22222 (EXACT)
的字符串<?php
$string = 'STRING'
$pattern = '#PATTER#' // what I have to find
echo preg_match($pattern, $string) ? 'match' : 'no match';
案例:
$string = '33333';// match
$string = '222222';// match
$string = '11111';// no match
$string = '22222';// no match
我尝试了很多我 google 和 none 有效的模式。
NOTE: 它必须是纯 REGEX 和 NOT 取反函数 preg_match
这个怎么样:
^(|.|..|...|....|(?!11111|22222).....|......+)$
基本思想是:所有长度为0-4和6+的字符串都可以。长度为5的字符串一定不能是11111或22222.
编辑:并稍微压缩长度(但降低 IMO 的可读性):
^(.{0,4}|(?!11111|22222).{5}|.{6}.*)$
怎么样:
^(?!11111$|22222$).*