Editing
Regular expression in Mandarin
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== 尋找英文字 === ==== 尋找 ASCII 字元 in MySQL ==== <pre> -- 尋找欄位 `my_column` 欄位值是 ASCII 字元 SELECT * FROM `my_table` WHERE `my_column` LIKE CONVERT(`my_column` USING ASCII) </pre> 相關文章 * [https://errerrors.blogspot.com/2020/07/search-app-not-apple-in-englishsearching.html 解決英文字的搜尋:搜尋 app 而不是 apple] 參考資料 * [https://stackoverflow.com/questions/401771/how-can-i-find-non-ascii-characters-in-mysql How can I find non-ASCII characters in MySQL? - Stack Overflow] ==== 尋找 ASCII 字元 in PHP ==== Code I: <pre> if (preg_match('/[^\x20-\x7f]/', $keyword) === 0) { echo "The keyword is ASCII only"; } else { echo "The keyword contains non-ASCII characters (like Chinese, Japanese, etc.)"; } </pre> Explanation: * {{kbd | key=<nowiki>/[^\x20-\x7f]/</nowiki>}} 是一個正規表達式: ** {{kbd | key=<nowiki>\x20</nowiki>}} 代表空格字符 (ASCII 32) ** {{kbd | key=<nowiki>\x7f</nowiki>}} 代表 DELETE 字符 (ASCII 127) ** {{kbd | key=<nowiki>[^\x20-\x7f]</nowiki>}} 表示「任何不在 \x20 到 \x7f 範圍內的字符」 這個範圍 {{kbd | key=<nowiki>(\x20-\x7f)</nowiki>}} 基本上涵蓋了所有可印刷的 ASCII 字符;不符合條件,則表示字串中包含非 ASCII 字符 (如中文、日文等) Code II<ref>[https://www.php.net/manual/en/regexp.reference.character-classes.php PHP: Character classes - Manual]</ref> <pre> $pattern = '/^[[:ascii:]]+$/i'; $text = "Hello World"; // ASCII only if (preg_match($pattern, $text)) { echo "純 ASCII 字元"; } else { echo "包含非 ASCII 字元"; } $text = "Hello 世界"; // Contains non-ASCII if (preg_match($pattern, $text)) { echo "純 ASCII 字元"; } else { echo "包含非 ASCII 字元"; } </pre> Explanation: * 匹配整個字串({{kbd | key=<nowiki>^</nowiki>}} 和 {{kbd | key=<nowiki>$</nowiki>}}) * 需要符合一個或多個 ASCII 字元({{kbd | key=<nowiki>[[:ascii:]]+</nowiki>}}) * 不區分大小寫({{kbd | key=<nowiki>i</nowiki>}} flag) ==== 尋找英文字、數字、破折號(-)或底線(_)字元 in MySQL ==== <pre> -- 尋找欄位 `my_column` 欄位值是包含英文字、數字、破折號(-)或底線(_)的字串 SELECT * FROM `my_table` WHERE `my_column` REGEXP '[a-zA-Z0-9\-_]' </pre>
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information