简体   繁体   English

没有连续空间RegEx Group PHP

[英]No Consecutive Spaces RegEx Group PHP

^([A-Za-z0-9 ]){8,12}

How do I turn this into something that can have spaces any where but not consecutively? 如何将其转换为可以在任何地方但不能连续地有空间的东西?

Id est: ID est:

A L L 0 W E D

N O T  A L L 0 W E D # Note two spaces between `not` and `allowed`

You can use lookahead/behind technique, like the following: '/^([A-Za-z0-9]| (?! )){8,12}/' . 您可以使用超前/后退技术,如下所示: '/^([A-Za-z0-9]| (?! )){8,12}/' It means we expect AZ, az and 0-9 OR space, but not followed by space. 这意味着我们期望AZ,az和0-9 OR空间,但不紧随其后。 Take a look at the result: 看一下结果:

$strs = array(
    '12345678',
    'A L L 0 W E D',
    'N O T  A L L 0 W E D' # Note two spaces between `not` and `allowed`
);

$preg = '/^([A-Za-z0-9]| (?! )){8,12}/';

foreach ($strs as $str) {
    var_dump(preg_match($preg, $str));
}

return;

It will return: 它将返回:

int(1)
int(1)
int(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM