如果第一个字母可能不是A-Z范围但是来自其他语言,并且如果第一个字符不是数字,那么如何检查字符串是否以大写字母开头?
的示例:
"This is string" - match
"this is string" - not match
"5 not good" - not match
"Увеличи starts capital" - match
"мащабиране no capital" - not match
在php中:
if (preg_math('/??/i', $str) ) {
echo 'yeeee haaa';
}
答案 0 :(得分:16)
使用此正则表达式:
preg_match('#^\p{Lu}#u', $str)
\p{Lu}
将匹配任何具有大写字母Unicode字符属性的字符。
Demo on regex101(请忽略标记m
和g
,它们仅用于演示目的)
当您处理Unicode字符串时(或者更具体地说,在preg_
函数的情况下,Unicode字符串必须采用UTF-8编码),您必须始终使用u
flag 使引擎处理输入字符串和带有字符语义的模式。否则,默认情况下,preg_
函数将模式和输入字符串视为字节数组,并为ASCII范围外的字符生成意外结果。
答案 1 :(得分:2)
怎么样:
if ($string{0} != mb_strtoupper($string{0}, 'UTF-8')) {
// not uppercase
}
答案 2 :(得分:1)
可能是这样的:
if (strtoupper($str[0]) == $str[0])
{
echo "match";
}
或:
if(ctype_upper($str[0]))
{
echo "match";
}
我不确定它们是否适用于所有字符集。