[英]PHP regexp for checking letters except W, w, P, p
I need a regexp pattern, that checks if string contains letters excepting W, w, P, p. 我需要一个正则表达式模式,该模式检查字符串是否包含除W,w,P,p以外的字母。
$pattern = ''; // I need this pattern
preg_match($pattern, '123123'); // false
preg_match($pattern, '123123a'); // true
preg_match($pattern, '123123W'); // false
preg_match($pattern, '123123w'); // false
preg_match($pattern, '123123P'); // false
preg_match($pattern, '123123p'); // false
preg_match($pattern, '123123WwPp'); // false
preg_match($pattern, 'abcWwPp'); // true
preg_match($pattern, 'abc'); // true
Thank you in advance. 先感谢您。
If you only care for ASCII letters, check for 如果您只关心ASCII字母,请检查
[^\W\d_WP]
and make the search case-insensitive: 并使搜索不区分大小写:
preg_match('/[^\W\d_WP]/i', $subject)
[^\\W\\d_WP]
matches a character that is alphanumeric, substracting digits, underscore, W
and P
from the list of allowed characters. [^\\W\\d_WP]
匹配一个字母数字字符,在允许的字符列表中减去数字,下划线, W
和P
[^\\W]
looks counterintuitive since it means "not a non-alphanumeric character", but that double negative pays off because I can then substract other characters from the result. [^\\W]
看起来违反直觉,因为它的意思是“不是非字母数字字符”,但是双负数会有所回报,因为我可以从结果中减去其他字符。
If you care about Unicode letters, use 如果您关心Unicode字母,请使用
preg_match('/[^\PLWP]/iu', $subject)
\\PL
matches any character that is not a Unicode letter (opposite of \\pL
) \\PL
匹配不是Unicode字母的任何字符(与\\pL
相反)
Search for the range outside of w and p - like this 搜索w和p之外的范围-像这样
/[a-oq-vx-z]/i
Note the i
at the end for case insensitive 请注意末尾的i
,以区分大小写
This is not an answer but a little program to check Tim Pietzcker expression: it works according to the test provided by Billy. 这不是答案,而是一个检查Tim Pietzcker表达式的小程序:它根据Billy提供的测试进行工作。
<?php
error_reporting(E_ALL);
$pattern = '/[^\W\d_WP]/i';
assert(!preg_match($pattern, '123123'));
assert(preg_match($pattern, '123123a'));
assert(!preg_match($pattern, '123123W'));
assert(!preg_match($pattern, '123123w'));
assert(!preg_match($pattern, '123123P'));
assert(!preg_match($pattern, '123123p'));
assert(!preg_match($pattern, '123123WwPp'));
assert(preg_match($pattern, 'abcWwPp'));
assert(preg_match($pattern, 'abc'));
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.