字符串中至少一个大写字符
Minimum one upper character in string
如何检查字符串在 UTF-8 编码中是否至少包含一个大写字符?我用 preg_match
检查了这个
preg_match('/[A-Z]/', $var)
但此代码不适用于所有字符,例如 Ó
、Ł
。
我该如何解决?
也尝试添加 \p{Lu}\p{Lt}
模式,使用 unicode
标志:
preg_match('/[A-Z\p{Lu}\p{Lt}]/u', $var)
A-Z
正在 ascii 范围之间查找。您显示的字符超出了该范围。使用 \p{Lu}
并使用 unicode 修饰符 u
.
preg_match('/\p{Lu}/u', $var)
演示:https://regex101.com/r/WZaOCD/1/
有关更多 unicode 选项,请参阅 http://php.net/manual/en/regexp.reference.unicode.php。
如何检查字符串在 UTF-8 编码中是否至少包含一个大写字符?我用 preg_match
preg_match('/[A-Z]/', $var)
但此代码不适用于所有字符,例如 Ó
、Ł
。
我该如何解决?
也尝试添加 \p{Lu}\p{Lt}
模式,使用 unicode
标志:
preg_match('/[A-Z\p{Lu}\p{Lt}]/u', $var)
A-Z
正在 ascii 范围之间查找。您显示的字符超出了该范围。使用 \p{Lu}
并使用 unicode 修饰符 u
.
preg_match('/\p{Lu}/u', $var)
演示:https://regex101.com/r/WZaOCD/1/
有关更多 unicode 选项,请参阅 http://php.net/manual/en/regexp.reference.unicode.php。