正则表达式:小写、点、零空格 [英] Regular Expressions: low-caps, dots, zero spaces
问题描述
如何编写一个表达式来检查字符串中是否存在低大写字母、点和空格?
how do I write an expression which checks for lowcaps, dots, and without any white space in the string?
到目前为止,下面的代码试图检查低大写字母和点(无论如何它都不起作用!)但我不知道如何添加空格的表达式.
the code below so far was trying to check for lowcaps and dots (it does not work anyway!) but I don't know how to add in the expression for white spaces.
# check for matches of lowcaps or lowcaps with a dot
if (!preg_match('/([a-z0-9]|[a-z0-9\.])/', $cst_value))
{
$error = true;
echo ' please use lowcaps only with dot(s) and without any spacing.';
}
推荐答案
[a-z0-9.]
匹配小写字母、数字或点.[^a-z0-9.]
匹配所有不是小写字母、数字或点的字符.
因此,如果 /[^a-z0-9.]/
匹配字符串包含除 lc-letter、digit 或 dot 以外的任何内容的任何位置.如果不匹配,则满足您的条件.
[a-z0-9.]
matches a lower-case letter or a digit or a dot.
[^a-z0-9.]
matches all characters that are not a lower-case letter or a digit or a dot.
So if /[^a-z0-9.]/
matches anywhere the string contains something other than lc-letter,digit or dot. If it does not match your condition is fulfilled.
if ( !preg_match('/[^a-z0-9.]/', $cst_value) ) {
// only lower-case letters, digits or dots
}
或不带数字
if ( !preg_match('/[^a-z.]/', $cst_value) ) {
// only lower-case letters or dots
}
<小时>
更新:示例:
update: example:
foreach( array('abcdef', 'abc de', 'abc.de', 'aBcde') as $cst_value) {
echo $cst_value, ': ';
if ( !preg_match('/[^a-z.]/', $cst_value) ) {
echo " ok.\n";
}
else {
echo "failure\n";
}
}
印刷品
abcdef: ok.
abc de: failure
abc.de: ok.
aBcde: failure
这篇关于正则表达式:小写、点、零空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!