問題描述
我需要正則表達式方面的幫助.我的字符串包含 unicode 字符,下面的代碼不起作用.
I need help with regular expressions. My string contains unicode characters and code below doesn't work.
前四個字符必須是數字,然后是逗號,然后是任何字母字符或空格...我已經讀過,如果我在常規表達式的末尾添加/u 但它對我不起作用...
First four characters must be numbers, then comma and then any alphabetic characters or whitespaces... I already read that if i add /u on end of regular expresion but it didn't work for me...
我的代碼適用于非 unicode 字符
My code works with non-unicode characters
$post = '9999,?kofja loka';;
echo preg_match('/^[0-9]{4},[s]*[a-zA-Z]+', $post);
感謝您的回答!
推薦答案
更新答案:
這是現在測試和工作
Updated answer:
This is now tested and working
$post = '9999, ?kofja loka';
echo preg_match('/^\d{4},[\s\p{L}]+$/u', $post);
\w
將不起作用,因為它不包含所有 unicode 字母,并且除了字母之外還包含 [0-9_]
.
\w
will not work, because it does not contain all unicode letters and contains also [0-9_]
additionally to the letters.
重要的還有 u
修飾符來激活 unicode 模式.
Important is also the u
modifier to activate the unicode mode.
如果逗號后可以有字母 或 空格,那么您應該將它們放入相同的字符類中,在您的正則表達式中,逗號后有 0 個或多個空格,然后只有字母.
If there can be letters or whitespace after the comma then you should put those into the same character class, in your regex there are 0 or more whitespace after the comma and then there are only letters.
參見http://www.regular-expressions.info/php.html用于 php 正則表達式的詳細信息
See http://www.regular-expressions.info/php.html for php regex details
\p{L}
(Unicode 字母)解釋 這里
The \p{L}
(Unicode letter) is explained here
重要的還有使用字符串邊界的結尾 $
來確保真正完整的字符串被驗證,否則它只會匹配第一個空格而忽略其余的例如.
Important is also the use of the end of string boundary $
to ensure that really the complete string is verified, otherwise it will match only the first whitespace and ignore the rest for example.
這篇關于PHP 正則表達式中的 UTF-8的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!