MySQL 正则表达式与 REGEXP 算子MySQL 正则表达式与 REGEXP 算子MySQL 正则表达式与 REGEXP 算子MySQL 正则表达式与 REGEXP 算子
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
MySQL 正则表达式与 REGEXP 算子
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式工具和实用程序 » MySQL 正则表达式与 REGEXP 算子

正则表达式工具
grep
语言和函数库
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery 和 XPath
XRegExp
数据库
MySQL
Oracle
PostgreSQL
更多内容
简介
正则表达式快速入门
正则表达式教程
取代字符串教学
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

MySQL 正则表达式与 REGEXP 算子

MySQL 对正则表达式的支持相当有限,但仍然非常有用。MySQL 仅有一个算子可让您使用正则表达式。这是 REGEXP 算子,其运作方式就像 LIKE 算子,只不过它不使用 _ 和 % 通配符,而是使用 POSIX 扩展正则表达式 (ERE)。尽管标准名称中含有「延伸」,但 POSIX ERE 风格在现代标准中是一种相当基本的正则表达式风格。尽管如此,它让 REGEXP 算子比简单的 LIKE 算子强大且灵活许多。

LIKE 和 REGEXP 算子之间的一个重要差异是,只有当样式与整个字符串相符时,LIKE 算子才会传回 True。例如,WHERE testcolumn LIKE 'jg' 只会传回 testcolumn 与 jg 相同的列,除了可能大小写不同。另一方面,WHERE testcolumn REGEXP 'jg' 会传回 testcolumn 在字符串中任何位置都有 jg 的所有列。使用 WHERE testcolumn REGEXP '^jg$' 仅取得与 jg 相同的栏。等同于 WHERE testcolumn LIKE 'jg%' 的会是 WHERE testcolumn REGEXP '^jg'。不需要在正则表达式的结尾加上 .*(REGEXP 等同于 LIKE 的 %),因为接受部分相符。

MySQL 不提供任何 相符模式。POSIX ERE 不支持正则表达式内的模式修改器,而 MySQL 的 REGEXP 算子不提供在正则表达式外指定模式的方式。点 会相符所有字符,包括换行符,而 插入符号和美元符号 仅会在字符串的最开始和最后相符。换句话说:MySQL 将换行字符视为一般字符。如果数据表的校对是大小写不敏感的,则 REGEXP 算子会以大小写不敏感的方式套用正则表达式,这是默认值。如果您将校对变更为大小写敏感,则 REGEXP 算子会变成大小写敏感。

请记住,MySQL 支持字符串中的 C 式转义串行。尽管 POSIX ERE 不支持 \n 等代码来比对换行等不可打印字符,MySQL 却支持字符串中的此转义。因此,WHERE testcolumn REGEXP '\n' 会传回 testcolumn 中包含换行的所有列。MySQL 会在剖析正则表达式之前,将字符串中的 \n 转换成单一换行字符。这也表示反斜线需要转义。比对单一反斜线的正则表达式 \\ 在 MySQL 字符串中会变成 '\\\\',而比对美元符号的正则表达式 \$ 在 MySQL 字符串中会变成 '\\$'。这与其他数据库(例如 Oracle)不同,后者不支持 \n,也不需要转义反斜线。

若要传回字段与正则表达式不符的列,请使用 WHERE testcolumn NOT REGEXP 'pattern'。RLIKE 算子是 REGEXP 算子的同义词。WHERE testcolumn RLIKE 'pattern' 和 WHERE testcolumn NOT RLIKE 'pattern' 与 WHERE testcolumn REGEXP 'pattern' 和 WHERE testcolumn NOT REGEXP 'pattern' 相同。我建议您使用 REGEXP 而非 RLIKE,以避免与 LIKE 算子混淆。

LIB_MYSQLUDF_PREG

如果您想要在数据库中拥有更强大的正则表达式,可以考虑使用 LIB_MYSQLUDF_PREG。这是一个 MySQL 用户函数的开放原代码函数库,用于导入 PCRE 函数库。LIB_MYSQLUDF_PREG 仅以原代码形式提供。若要使用它,您需要能够将其编译并安装到您的 MySQL 服务器中。安装此函数库不会以任何方式变更 MySQL 内置的正则表达式支持。它仅提供下列其他函数

PREG_CAPTURE 从字符串中萃取正则表达式比对。PREG_POSITION 传回正则表达式比对字符串的位置。PREG_REPLACE 对字符串运行搜索并取代。PREG_RLIKE 测试正则表达式是否比对字符串。

所有这些函数都将正则表达式作为其第一个参数。此正则表达式必须格式化为 Perl 正则表达式操作符。例如,要测试 regex 是否与主旨不分大小写地匹配,您会使用 MySQL 代码 PREG_RLIKE('/regex/i', subject)。这类似于 PHP 的 preg 函数,它也需要 PHP 字符串内正则表达式的额外 // 分隔符号。

MySQL 正則表示式與 REGEXP 算子
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式工具和實用程式 » MySQL 正則表示式與 REGEXP 算子

正規表示式工具
grep
語言和函式庫
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery 和 XPath
XRegExp
資料庫
MySQL
Oracle
PostgreSQL
本網站的更多資訊
簡介
正規表示式快速入門
正規表示式教學
取代字串教學
應用程式和語言
正規表示式範例
正規表示式參考
取代字串參考

MySQL 正則表示式與 REGEXP 算子

MySQL 對正規表示式的支援相當有限,但仍然非常有用。MySQL 僅有一個算子可讓您使用正規表示式。這是 REGEXP 算子,其運作方式就像 LIKE 算子,只不過它不使用 _ 和 % 萬用字元,而是使用 POSIX 延伸正規表示式 (ERE)。儘管標準名稱中含有「延伸」,但 POSIX ERE 風格在現代標準中是一種相當基本的正規表示式風格。儘管如此,它讓 REGEXP 算子比簡單的 LIKE 算子強大且靈活許多。

LIKE 和 REGEXP 算子之間的一個重要差異是,只有當樣式與整個字串相符時,LIKE 算子才會傳回 True。例如,WHERE testcolumn LIKE 'jg' 只會傳回 testcolumn 與 jg 相同的列,除了可能大小寫不同。另一方面,WHERE testcolumn REGEXP 'jg' 會傳回 testcolumn 在字串中任何位置都有 jg 的所有列。使用 WHERE testcolumn REGEXP '^jg$' 僅取得與 jg 相同的欄。等同於 WHERE testcolumn LIKE 'jg%' 的會是 WHERE testcolumn REGEXP '^jg'。不需要在正規表示式的結尾加上 .*(REGEXP 等同於 LIKE 的 %),因為接受部分相符。

MySQL 不提供任何 相符模式。POSIX ERE 不支援正規表示式內的模式修改器,而 MySQL 的 REGEXP 算子不提供在正規表示式外指定模式的方式。點 會相符所有字元,包括換行符,而 插入符號和美元符號 僅會在字串的最開始和最後相符。換句話說:MySQL 將換行字元視為一般字元。如果資料表的校對是大小寫不敏感的,則 REGEXP 算子會以大小寫不敏感的方式套用正規表示式,這是預設值。如果您將校對變更為大小寫敏感,則 REGEXP 算子會變成大小寫敏感。

請記住,MySQL 支援字串中的 C 式跳脫序列。儘管 POSIX ERE 不支援 \n 等代碼來比對換行等不可列印字元,MySQL 卻支援字串中的此跳脫。因此,WHERE testcolumn REGEXP '\n' 會傳回 testcolumn 中包含換行的所有列。MySQL 會在剖析正規表示式之前,將字串中的 \n 轉換成單一換行字元。這也表示反斜線需要跳脫。比對單一反斜線的正規表示式 \\ 在 MySQL 字串中會變成 '\\\\',而比對美元符號的正規表示式 \$ 在 MySQL 字串中會變成 '\\$'。這與其他資料庫(例如 Oracle)不同,後者不支援 \n,也不需要跳脫反斜線。

若要傳回欄位與正規表示式不符的列,請使用 WHERE testcolumn NOT REGEXP 'pattern'。RLIKE 算子是 REGEXP 算子的同義詞。WHERE testcolumn RLIKE 'pattern' 和 WHERE testcolumn NOT RLIKE 'pattern' 與 WHERE testcolumn REGEXP 'pattern' 和 WHERE testcolumn NOT REGEXP 'pattern' 相同。我建議您使用 REGEXP 而非 RLIKE,以避免與 LIKE 算子混淆。

LIB_MYSQLUDF_PREG

如果您想要在資料庫中擁有更強大的正規表示式,可以考慮使用 LIB_MYSQLUDF_PREG。這是一個 MySQL 使用者函數的開放原始碼函式庫,用於匯入 PCRE 函式庫。LIB_MYSQLUDF_PREG 僅以原始碼形式提供。若要使用它,您需要能夠將其編譯並安裝到您的 MySQL 伺服器中。安裝此函式庫不會以任何方式變更 MySQL 內建的正規表示式支援。它僅提供下列其他函數

PREG_CAPTURE 從字串中萃取正規表示式比對。PREG_POSITION 傳回正規表示式比對字串的位置。PREG_REPLACE 對字串執行搜尋並取代。PREG_RLIKE 測試正規表示式是否比對字串。

所有這些函數都將正規表示式作為其第一個參數。此正規表示式必須格式化為 Perl 正規表示式運算子。例如,要測試 regex 是否與主旨不分大小寫地匹配,您會使用 MySQL 程式碼 PREG_RLIKE('/regex/i', subject)。這類似於 PHP 的 preg 函數,它也需要 PHP 字串內正規表示式的額外 // 分隔符號。

MySQL Regular Expressions with The REGEXP Operator
  • 简
  • 繁
  • En
About Regular Expressions » Tools and Utilities for Regular Expressions » MySQL Regular Expressions with The REGEXP Operator

Regex Tools
grep
Languages & Libraries
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery & XPath
XRegExp
Databases
MySQL
Oracle
PostgreSQL
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

MySQL Regular Expressions with The REGEXP Operator

MySQL’s support for regular expressions is rather limited, but still very useful. MySQL only has one operator that allows you to work with regular expressions. This is the REGEXP operator, which works just like the LIKE operator, except that instead of using the _ and % wildcards, it uses a POSIX Extended Regular Expression (ERE). Despite the “extended” in the name of the standard, the POSIX ERE flavor is a fairly basic regex flavor by modern standards. Still, it makes the REGEXP operator far more powerful and flexible than the simple LIKE operator.

One important difference between the LIKE and REGEXP operators is that the LIKE operator only returns True if the pattern matches the whole string. E.g. WHERE testcolumn LIKE 'jg' will return only rows where testcolumn is identical to jg, except for differences in case perhaps. On the other hand, WHERE testcolumn REGEXP 'jg' will return all rows where testcolumn has jg anywhere in the string. Use WHERE testcolumn REGEXP '^jg$' to get only columns identical to jg. The equivalent of WHERE testcolumn LIKE 'jg%' would be WHERE testcolumn REGEXP '^jg'. There’s no need to put a .* at the end of the regex (the REGEXP equivalent of LIKE’s %), since partial matches are accepted.

MySQL does not offer any matching modes. POSIX EREs don’t support mode modifiers inside the regular expression, and MySQL’s REGEXP operator does not provide a way to specify modes outside the regular expression. The dot matches all characters including newlines, and the caret and dollar only match at the very start and end of the string. In other words: MySQL treats newline characters like ordinary characters. The REGEXP operator applies regular expressions case insensitively if the collation of the table is case insensitive, which is the default. If you change the collation to be case sensitive, the REGEXP operator becomes case sensitive.

Remember that MySQL supports C-style escape sequences in strings. While POSIX ERE does not support tokens like \n to match non-printable characters like line breaks, MySQL does support this escape in its strings. So WHERE testcolumn REGEXP '\n' returns all rows where testcolumn contains a line break. MySQL converts the \n in the string into a single line break character before parsing the regular expression. This also means that backslashes need to be escaped. The regex \\ to match a single backslash becomes '\\\\' as a MySQL string, and the regex \$ to match a dollar symbol becomes '\\$' as a MySQL string. All this is unlike other databases like Oracle, which don’t support \n and don’t require backslashes to be escaped.

To return rows where the column doesn’t match the regular expression, use WHERE testcolumn NOT REGEXP 'pattern' The RLIKE operator is a synonym of the REGEXP operator. WHERE testcolumn RLIKE 'pattern' and WHERE testcolumn NOT RLIKE 'pattern' are identical to WHERE testcolumn REGEXP 'pattern' and WHERE testcolumn NOT REGEXP 'pattern'. I recommend you use REGEXP instead of RLIKE, to avoid confusion with the LIKE operator.

LIB_MYSQLUDF_PREG

If you want more regular expression power in your database, you can consider using LIB_MYSQLUDF_PREG. This is an open source library of MySQL user functions that imports the PCRE library. LIB_MYSQLUDF_PREG is delivered in source code form only. To use it, you’ll need to be able to compile it and install it into your MySQL server. Installing this library does not change MySQL’s built-in regex support in any way. It merely makes the following additional functions available:

PREG_CAPTURE extracts a regex match from a string. PREG_POSITION returns the position at which a regular expression matches a string. PREG_REPLACE performs a search-and-replace on a string. PREG_RLIKE tests whether a regex matches a string.

All these functions take a regular expression as their first parameter. This regular expression must be formatted like a Perl regular expression operator. E.g. to test if regex matches the subject case insensitively, you’d use the MySQL code PREG_RLIKE('/regex/i', subject). This is similar to PHP’s preg functions, which also require the extra // delimiters for regular expressions inside the PHP string.

©2015-2025 艾丽卡 support@alaica.com