我需要用PHP编写一个正则表达式,条件是
A-Z, a-z, 0-9
,
:
,
-
,
_
,但字符串末尾不能为
:
这就是我试过的
<?php
$strings = [
'aaa:bbb-cool',
'aaa:bbb-cool-',
'aaa-22-bbb_cool3',
'aaa:bbb-cool:',
'aaa_bbb-cool:',
'aaa_bbb-cool',
'bbbb:>dd',
'hihi%',
'大家好',
'0000000000',
'11111:2222:3333',
'11111:2222:3333:',
'DDD@@@1',
'大家好',
];
$pattern = '/[0-9a-zA-Z]+$/i';
foreach ($strings as $key => $string) {
var_dump('number '.$key .' '. $string.' is '.preg_match($pattern, $string));
}
结果是
string(26) "number 0 aaa:bbb-cool is 1"
string(27) "number 1 aaa:bbb-cool- is 0"
string(30) "number 2 aaa-22-bbb_cool3 is 1"
string(27) "number 3 aaa:bbb-cool: is 0"
string(27) "number 4 aaa_bbb-cool: is 0"
string(26) "number 5 aaa_bbb-cool is 1"
string(22) "number 6 bbbb:>dd is 1"
string(19) "number 7 hihi% is 0"
string(23) "number 8 大家好 is 0"
string(24) "number 9 0000000000 is 1"
string(30) "number 10 11111:2222:3333 is 1"
string(31) "number 11 11111:2222:3333: is 0"
string(22) "number 12 DDD@@@1 is 1"
string(24) "number 13 大家好 is 0"
-
我知道我并不是真的阻止”:“最终,原因1应该是真的,如何纠正它
-
为什么数字6和数字12是真的?