亚洲成A人片在线观看网站_成年网站免费视频A在线双飞_日日日日做夜夜夜夜无码_久久夜色撩人精品国产小说

RegEX 備忘清單

正則表達式 (regex) 的快速參考,包括符號、范圍、分組、斷言和一些示例模式,以幫助您入門。

入門

介紹

這是開始使用正則表達式(Regex)的快速備忘單。

字符類

范例說明
[abc]單個字符:abc
[^abc]一個字符,除了:abc
[a-z]范圍內的字符:a-z
[^a-z]不在范圍內的字符:a-z
[0-9]范圍內的數字:0-9
[a-zA-Z]范圍內的字符:
a-zA-Z
[a-zA-Z0-9]范圍內的字符:
a-zA-Z0-9

量詞

范例說明
a?零個或一個a
a*零個或多個 a
a+一個或多個a
[0-9]+0-9中的一個或多個
a{3}正好 3a
a{3,}3個或更多的a
a{3,6}a36 之間
a*貪心量詞
a*?惰性量詞
a*+占有量詞

常用元字符

  • ^
  • {
  • +
  • <
  • [
  • *
  • )
  • >
  • .
  • (
  • |
  • $
  • \
  • ?

使用 \ 轉義這些特殊字符

元序列

范例說明
.任何單個字符
\s任何空白字符
\S任何非空白字符
\d任何數字,與 [0-9] 相同
\D任何非數字,與 [^0-9] 相同
\w任何單詞字符
\W任何非單詞字符
\X任何 Unicode 序列,包括換行符
\C匹配一個數據單元
\RUnicode 換行符
\v垂直空白字符
\V\v 的否定 - 除了換行符和垂直制表符之外的任何內容
\h水平空白字符
\H\h 的否定
\K重置匹配
\n匹配第 n 個子模式
\pXUnicode 屬性 X
\p{...}Unicode 屬性或腳本類別
\PX\pX 的否定
\P{...}\p 的否定
\Q...\E引用;視為文字
\k<name>匹配子模式name
\k'name'匹配子模式name
\k{name}匹配子模式name
\gn匹配第 n 個子模式
\g{n}匹配第 n 個子模式
\g<n>遞歸第 n 個捕獲組
\g'n'遞歸第 n 個捕獲組。
\g{-n}匹配第 n 個相對前一個子模式
\g<+n>遞歸第 n 個相對即將到來的子模式
\g'+n'匹配第 n 個相對即將到來的子模式
\g'letter'遞歸命名捕獲組 字母
\g{letter}匹配先前命名的捕獲組 字母
\g<letter>遞歸命名捕獲組 字母
\xYY十六進制字符 YY
\x{YYYY}十六進制字符 YYYY
\ddd八進制字符ddd
\cY控制字符 Y
[\b]退格字符
\使任何字符文字

錨點

范例說明
\G比賽開始
^字符串的開始
$字符串結束
\A字符串的開始
\Z字符串結束
\z字符串的絕對結尾
\b一個詞的邊界
\B非單詞邊界

替代

范例說明
\0完整的比賽內容
\1捕獲組 1 中的內容
$1捕獲組 1 中的內容
${foo}捕獲組 foo 中的內容
\x20十六進制替換值
\x{06fa}十六進制替換值
\t標簽
\r回車
\n新隊
\f換頁
\U大寫轉換
\L小寫轉換
\E終止任何轉換

組構造

范例說明
(...)捕獲所有封閉的東西
(a|b)匹配 ab
(?:...)匹配隨附的所有內容
(?>...)原子組(非捕獲)
(?|...)重復的子模式組號
(?#...)注解
(?'name'...)命名捕獲組
(?<name>...)命名捕獲組
(?P<name>...)命名捕獲組
(?imsxXU)內聯修飾符
(?(DEFINE)...)在使用它們之前預定義模式

斷言

:--
(?(1)yes|no)條件語句
(?(R)yes|no)條件語句
(?(R#)yes|no)遞歸條件語句
(?(R&name)yes|no)條件語句
(?(?=...)yes|no)有條件的前瞻
(?(?<=...)yes|no)有條件的往后看

遞歸

:--
(?R)遞歸整個模式
(?1)遞歸第一個子模式
(?+1)遞歸第一個相對子模式
(?&name)遞歸子模式name
(?P=name)匹配子模式name
(?P>name)遞歸子模式name

標志/修飾符

:--
g全部
m多行
i不區分大小寫
x忽略空格
s單線
u統一碼
X擴展
U不貪心
A
J重復的組名
d結果包含捕獲組子字符串開始和結束的索引

零寬度斷言

:--
(?=...)正先行斷言
(?!...)負先行斷言
(?<=...)正后發斷言
(?<!...)負后發斷言
?=正先行斷言-存在
?!負先行斷言-排除
?<=正后發斷言-存在
?<!負后發斷言-排除

零寬度斷言 允許您在主模式之前(向后看)或之后(lookahead)匹配一個組,而不會將其包含在結果中。

POSIX 字符類

字符類如同意義
[[:alnum:]][0-9A-Za-z]字母和數字
[[:alpha:]][A-Za-z]字母
[[:ascii:]][\x00-\x7F]ASCII 碼 0-127
[[:blank:]][\t ]僅空格或制表符
[[:cntrl:]][\x00-\x1F\x7F]控制字符
[[:digit:]][0-9]十進制數字
[[:graph:]][[:alnum:][:punct:]]可見字符(不是空格)
[[:lower:]][a-z]小寫字母
[[:print:]][ -~] == [ [:graph:]]可見字符
[[:punct:]][!"#$%&’()*+,-./:;<=>?@[]^_`{|}~]可見標點符號
[[:space:]][\t\n\v\f\r ]空白
[[:upper:]][A-Z]大寫字母
[[:word:]][0-9A-Za-z_]單詞字符
[[:xdigit:]][0-9A-Fa-f]十六進制數字
[[:<:]][\b(?=\w)]詞的開頭
[[:>:]][\b(?<=\w)]詞尾

控制動詞

:--
(*ACCEPT)控制動詞
(*FAIL)控制動詞
(*MARK:NAME)控制動詞
(*COMMIT)控制動詞
(*PRUNE)控制動詞
(*SKIP)控制動詞
(*THEN)控制動詞
(*UTF)圖案修飾符
(*UTF8)圖案修飾符
(*UTF16)圖案修飾符
(*UTF32)圖案修飾符
(*UCP)圖案修飾符
(*CR)換行修飾符
(*LF)換行修飾符
(*CRLF)換行修飾符
(*ANYCRLF)換行修飾符
(*ANY)換行修飾符
\R換行修飾符
(*BSR_ANYCRLF)換行修飾符
(*BSR_UNICODE)換行修飾符
(*LIMIT_MATCH=x)正則表達式引擎修飾符
(*LIMIT_RECURSION=d)正則表達式引擎修飾符
(*NO_AUTO_POSSESS)正則表達式引擎修飾符
(*NO_START_OPT)正則表達式引擎修飾符

正則表達式示例

字符串

范例說明
ring匹配 ring springboard 等。
.匹配 a9+ 等。
h.o匹配 hooh2oh/o 等。
ring\?匹配 ring?
\(quiet\)匹配(安靜)
c:\\windows匹配 c:\windows

使用 \ 搜索這些特殊字符:
[ \ ^ $ . | ? * + ( ) { }

速記類

范例說明
\w“單詞”字符
(字母、數字或下劃線)
\d數字
\s空格
(空格、制表符、vtab、換行符)
\W, \D, or \S不是單詞、數字或空格
[\D\S]表示不是數字或空格,兩者都匹配
[^\d\s]禁止數字和空格

出現次數

范例說明
colou?r匹配 colorcolour
[BW]ill[ieamy's]*匹配 BillWillyWilliam's 等。
[a-zA-Z]+匹配 1 個或多個字母
\d{3}-\d{2}-\d{4}匹配 SSN
[a-z]\w{1,7}匹配 UW NetID

備擇方案

范例說明
cat|dog匹配 catdog
id|identity匹配 ididentity
identity|id匹配 ididentity

當替代品重疊時,命令從長到短

字符類

范例說明
[aeiou]匹配任何元音
[^aeiou]匹配一個非元音
r[iau]ng匹配ring、wrangle、sprung等。
gr[ae]y匹配 graygrey
[a-zA-Z0-9]匹配任何字母或數字

[ ] 中總是轉義 . \ ] 有時是 ^ - .

貪婪與懶惰

范例說明
* + {n,}
greedy
盡可能匹配
<.+><b>bold</b> 中找到 1 個大匹配項
*? +? {n,}?
lazy
盡可能少匹配
<.+?>在 <b>bold</b> 中找到 2 個匹配項

范圍

范例說明
\b“單詞”邊緣(非“單詞”字符旁邊)
\bring單詞以“ring”開頭,例如 ringtone
ring\b單詞以“ring”結尾,例如 spring
\b9\b匹配單個數字 9,而不是 19、91、99 等。
\b[a-zA-Z]{6}\b匹配 6 個字母的單詞
\B不是字邊
\Bring\B匹配 springswringer
^\d*$整個字符串必須是數字
^[a-zA-Z]{4,20}$字符串必須有 4-20 個字母
^[A-Z]字符串必須以大寫字母開頭
[\.!?"')]$字符串必須以終端標點結尾

修飾

范例說明
(?i)[a-z]*(?-i)忽略大小寫開/關
(?s).*(?-s)匹配多行(導致 . 匹配換行符)
(?m)^.*;$(?-m)^ & $ 匹配行不是整個字符串
(?x)#free-spacing 模式,此 EOL 注釋被忽略
(?-x)自由空間模式關閉
/regex/ismx修改整個字符串的模式

范例說明
(in|out)put匹配 inputoutput
\d{5}(-\d{4})?美國郵政編碼 (“+ 4”可選)

如果組后匹配失敗,解析器會嘗試每個替代方案。
可能導致災難性的回溯。

反向引用

范例說明
(to) (be) or not \1 \2匹配 to be or not to be
([^\s])\1{2}匹配非空格,然后再相同兩次 ? aaa, ...
\b(\w+)\s+\1\b匹配雙字

非捕獲組

范例說明
on(?:click|load)快于:on(click|load)

盡可能使用非捕獲或原子組

原子組

范例說明
(?>red|green|blue)比非捕獲更快
(?>id|identity)\b匹配 id,但不匹配 identity

"id" 匹配,但 \b 在原子組之后失敗, 解析器不會回溯到組以重試“身份”

如果替代品重疊,請從長到短命令。

零寬度斷言 Lookaround(前后預查)

范例說明
(?= )向前看,如果你能提前找到
(?! )向前看,如果你找不到前面
(?<= )向后看,如果你能找到后面
(?<! )向后看,如果你找不到后面
\b\w+?(?=ing\b)匹配 warbling, string, fishing, ...
\b(?!\w+ing\b)\w+\b不以“ing”結尾的單詞
(?<=\bpre).*?\b匹配 pretend、present、prefix、...
\b\w{3}(?<!pre)\w*?\b不以“pre”開頭的詞
\b\w+(?<!ing)\b匹配不以“ing”結尾的單詞

If-then-else

匹配 Mr.Ms. 如果單詞 her 稍后在字符串中

M(?(?=.*?\bher\b)s|r)\.

需要環顧 IF 條件

基礎實例

基本匹配

表達式匹配示例
theThe fat cat sat on the mat.
TheThe fat cat sat on the mat.

由字母t開始,接著是h,再接著是e

點運算符 .

表達式匹配示例
.arThe car parked in the garage.

表達式.ar匹配一個任意字符后面跟著是ar的字符串

字符集

表達式匹配示例
.arThe car parked in the garage.
ar[.]A garage is a good place to park a car.

方括號的句號就表示句號。表達式 ar[.] 匹配 ar. 字符串

否定字符集

表達式匹配示例
[^c]arThe car parked in the garage.

表達式 [^c]ar 匹配一個后面跟著 ar 的除了c的任意字符。

重復次數

*

表達式匹配示例
[a-z]*The car parked in the garage #21.
\s*cat\s*The fat cat sat on the concatenation.

表達式 [a-z]* 匹配一個行中所有以小寫字母開頭的字符串。

+

表達式匹配示例
c.+tThe fat cat sat on the mat.

表達式 c.+t 匹配以首字母c開頭以t結尾,中間跟著至少一個字符的字符串。

?

表達式匹配示例
[T]heThe car is parked in the garage.
[T]?heThe car is parked in the garage.

表達式 [T]?he 匹配字符串 heThe

{}

表達式匹配示例
[0-9]{2,3}The number was 9.9997 but we rounded it off to 10.0.
[0-9]{2,}The number was 9.9997 but we rounded it off to 10.0.
[0-9]{3}The number was 9.9997 but we rounded it off to 10.0.

(...) 特征標群

表達式匹配示例
(c|g|p)arThe car is parked in the garage.

表達式 (c|g|p)ar 匹配 cargarpar。 注意 \ 是在 Markdown 中為了不破壞表格轉義 |

| 或運算符

表達式匹配示例
(T|t)he|carThe car is parked in the garage.

表達式 (T|t)he|car 匹配 (T|t)hecar

轉碼特殊字符

表達式匹配示例
(f|c|m)at\.?The fat cat sat on the mat.

如果想要匹配句子中的 . 則要寫成 \. 以下這個例子 \.? 是選擇性匹配.

錨點

匹配指定開頭或結尾的字符串就要使用到錨點。

^ 號 (符串的開頭)

表達式匹配示例
(T|t)heThe car is parked in the garage.
^(T|t)heThe car is parked in the garage.

$ 號 (否是最后一個)

表達式匹配示例
(at\.)The fat cat. sat. on the mat.
(at\.)$The fat cat. sat. on the mat.

簡寫字符集

簡寫描述
.除換行符外的所有字符
\w匹配所有字母數字
等同于 [a-zA-Z0-9_]
\W匹配所有非字母數字,即符號
等同于: [^\w]
\d匹配數字: [0-9]
\D匹配非數字: [^\d]
\s匹配所有空格字符
等同于:[\t\n\f\r\p{Z}]
\S匹配所有非空格字符: [^\s]
\f匹配一個換頁符
\n匹配一個換行符
\r匹配一個回車符
\t匹配一個制表符
\v匹配一個垂直制表符
\p匹配 CR/LF(等同于 \r\n)
用來匹配 DOS 行終止符

正則表達式提供一些常用的字符集簡寫。

?=... 正先行斷言

表達式匹配示例
(T|t)he(?=\sfat)The fat cat sat on the mat.

Thethe 后面緊跟著 (空格)fat

?!... 負先行斷言

表達式匹配示例
(T|t)he(?!\sfat)The fat cat sat on the mat.

匹配 Thethe,且其后不跟著 (空格)fat

?<= ... 正后發斷言

表達式匹配示例
(?<=(T|t)he\s)(fat|mat)The fat cat sat on the mat.

匹配 fatmat,且其前跟著 Thethe

?<!... 負后發斷言

表達式匹配示例
(?<!(T|t)he\s)(cat)The cat sat on cat.

匹配 cat,且其前不跟著 Thethe

忽略大小寫 (Case Insensitive)

表達式匹配示例
TheThe fat cat sat on the mat.
/The/giThe fat cat sat on the mat.

修飾語 i 用于忽略大小寫,g 表示全局搜索。

表達式匹配示例
/.(at)/The fat cat sat on the mat.
/.(at)/gThe fat cat sat on the mat.

表達式 /.(at)/g 表示搜索 任意字符(除了換行)+ at,并返回全部結果。

多行修飾符 (Multiline)

表達式匹配示例
/.at(.)?$/The fat
cat sat
on the mat.
/.at(.)?$/gmThe fat
cat sat
on the mat.

貪婪匹配與惰性匹配 (Greedy vs lazy matching)

表達式匹配示例
/(.*at)/The fat cat sat on the mat.
/(.*?at)/The fat cat sat on the mat.

Python 中的正則表達式

入門

導入正則表達式模塊

import re

實例

re.search()

>>> sentence = 'This is a sample string'
>>> bool(re.search(r'this', sentence, flags=re.I))
True
>>> bool(re.search(r'xyz', sentence))
False

re.findall()

>>> re.findall(r'\bs?pare?\b', 'par spar apparent spare part pare')
['par', 'spar', 'spare', 'pare']
>>> re.findall(r'\b0*[1-9]\d{2,}\b', '0501 035 154 12 26 98234')
['0501', '154', '98234']

re.finditer()

>>> m_iter = re.finditer(r'[0-9]+', '45 349 651 593 4 204')
>>> [m[0] for m in m_iter if int(m[0]) < 350]
['45', '349', '4', '204']

re.split()

>>> re.split(r'\d+', 'Sample123string42with777numbers')
['Sample', 'string', 'with', 'numbers']

re.sub()

>>> ip_lines = "catapults\nconcatenate\ncat"
>>> print(re.sub(r'^', r'* ', ip_lines, flags=re.M))
* catapults
* concatenate
* cat

re.compile()

>>> pet = re.compile(r'dog')
>>> type(pet)
<class '_sre.SRE_Pattern'>
>>> bool(pet.search('They bought a dog'))
True
>>> bool(pet.search('A cat crossed their path'))
False

函數

函數說明
re.findall返回包含所有匹配項的列表
re.finditer返回一個可迭代的匹配對象
(每個匹配一個)
re.search如果字符串中的任何位置存在匹配項,則返回 Match 對象
re.split返回一個列表,其中字符串在每次匹配時被拆分
re.sub用字符串替換一個或多個匹配項
re.compile編譯正則表達式模式供以后使用
re.escape返回所有非字母數字反斜杠的字符串

Flags 標志

:---
re.Ire.IGNORECASE忽略大小寫
re.Mre.MULTILINE多行
re.Lre.LOCALE使 \w\b\s locale 依賴
re.Sre.DOTALL點匹配所有 (包括換行符)
re.Ure.UNICODE使 \w\b\d\s unicode 依賴
re.Xre.VERBOSE可讀風格

JavaScript 中的正則表達式

RegExp

屬性

:-:-
dotAll是否使用了 s 修飾符
flags返回標志的字符串
global是否使用了 g (全部)修飾符
hasIndices是否使用了 d 修飾符
ignoreCase匹配文本的時候是否忽略大小寫 i
multiline是否進行多行搜索 m
lastIndex該索引表示從哪里開始下一個匹配
source正則表達式的文本
sticky搜索是否是 sticky
unicodeUnicode 功能是否開啟

方法

:-:-
match()獲取匹配結果
matchAll()所有匹配項
replace()替換所有符合正則模式的匹配項
search()搜索以取得匹配正則模式的項
split()切割字符串返回字符串數組
compile()(重新)編譯正則表達式
exec()指定字符串中執行一個搜索匹配
test()正則表達式與指定的字符串是否匹配
toString()返回該正則表達式的字符串

test()

let textA = 'I like APPles very much';
let textB = 'I like APPles';
let regex = /apples$/i
 
console.log(regex.test(textA)); // false
console.log(regex.test(textB)); // true
let text = 'I like APPles very much';
let regexA = /apples/;
let regexB = /apples/i;

console.log(text.search(regexA)); // -1
console.log(text.search(regexB)); // 7

exec()

let text = 'Do you like apples?';
let regex= /apples/;
// Output: apples
console.log(regex.exec(text)[0]);
// Output: Do you like apples?
console.log(regex.exec(text).input);

match()

let text = 'Here are apples and apPleS';
let regex = /apples/gi;
 
// Output: [ "apples", "apPleS" ]
console.log(text.match(regex));

split()

let text = 'This 593 string will be brok294en at places where d1gits are.';
let regex = /\d+/g
 
// Output: [ "This ", " string will be brok", "en at places where d", "gits are." ] 
console.log(text.split(regex))

matchAll()

let regex = /t(e)(st(\d?))/g;
let text = 'test1test2';
let array = [...text.matchAll(regex)];
// Output: ["test1", "e", "st1", "1"]
console.log(array[0]);
// Output: ["test2", "e", "st2", "2"]
console.log(array[1]);

replace()

let text = 'Do you like aPPles?';
let regex = /apples/i
 
// Output: Do you like mangoes?
let result = text.replace(regex, 'mangoes');
console.log(result);

屬性示例

/d/s.dotAll;             // => true
/d/g.global;             // => true
/d/ig.flags;             // => "gi"
/d/d.hasIndices;         // => true
/d/i.ignoreCase;         // => true

多行文本中使用正則表達式

let s = "Please yes\nmake my day!";

s.match(/yes[^]*day/);
// 返回 'yes\nmake my day'

replaceAll()

let regex = /apples/gi;
let text = 'Here are apples and apPleS';

text.replaceAll(regex, "mangoes");
// 返回: Here are mangoes and mangoes

PHP中的正則表達式

函數

:--
preg_match()執行正則表達式匹配
preg_match_all()執行全局正則表達式匹配
preg_replace_callback()使用回調執行正則表達式搜索和替換
preg_replace()執行正則表達式搜索和替換
preg_split()按正則表達式模式拆分字符串
preg_grep()返回與模式匹配的數組條目

preg_replace

$str = "Visit Microsoft!";
$regex = "/microsoft/i";

// Output: Visit QuickRef!
echo preg_replace($regex, "QuickRef", $str); 

preg_match

$str = "Visit QuickRef";
$regex = "#quickref#i";
// Output: 1
echo preg_match($regex, $str);

preg_matchall

$regex = "/[a-zA-Z]+ (\d+)/";
$input_str = "June 24, August 13, and December 30";
if (preg_match_all($regex, $input_str, $matches_out)) {
    // Output: 2
    echo count($matches_out);
    // Output: 3
    echo count($matches_out[0]);
    // Output: Array("June 24", "August 13", "December 30")
    print_r($matches_out[0]);
    // Output: Array("24", "13", "30")
    print_r($matches_out[1]);
}

preg_grep

$arr = ["Jane", "jane", "Joan", "JANE"];
$regex = "/Jane/";
// Output: Jane
echo preg_grep($regex, $arr);

preg_split

$str = "Jane\tKate\nLucy Marion";
$regex = "@\s@";
// Output: Array("Jane", "Kate", "Lucy", "Marion")
print_r(preg_split($regex, $str));

Java 中的正則表達式

風格

第一種方式

Pattern p = Pattern.compile(".s", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("aS");  
boolean s1 = m.matches();  
System.out.println(s1);   // Outputs: true

第二種方式

boolean s2 = Pattern.compile("[0-9]+").matcher("123").matches();  
System.out.println(s2);   // Outputs: true

第三種方式

boolean s3 = Pattern.matches(".s", "XXXX");  
System.out.println(s3);   // Outputs: false

模式字段

:--
CANON_EQ規范等價
CASE_INSENSITIVE不區分大小寫的匹配
COMMENTS允許空格和注釋
DOTALL圓點模式
MULTILINE多行模式
UNICODE_CASEUnicode 感知大小寫折疊
UNIX_LINESUnix 行模式

方法

Pattern

  • 模式編譯 compile(字符串正則表達式 [,int flags])
  • 布爾匹配 matches([字符串正則表達式,] CharSequence 輸入)
  • String[] 拆分 split(字符串正則表達式 [,int 限制])
  • 字符串引用 quote(字符串 s)

匹配器

  • int start([int group | 字符串名稱])
  • int end([int group | 字符串名稱])
  • 布爾 find([int start])
  • 字符 group([int 組 | 字符串名稱])
  • 匹配器重置 reset()

String

  • boolean matches(String regex)
  • String replaceAll(String regex, 字符串替換)
  • String[] split(String regex[, int limit])

還有更多方法...

例子

替換句子:

String regex = "[A-Z\n]{5}$";
String str = "I like APP\nLE";
Pattern p = Pattern.compile(regex, Pattern.MULTILINE);
Matcher m = p.matcher(str);
// Outputs: I like Apple!
System.out.println(m.replaceAll("pple!"));

所有匹配的數組:

String str = "She sells seashells by the Seashore";
String regex = "\\w*se\\w*";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);
List<String> matches = new ArrayList<>();
while (m.find()) {
    matches.add(m.group());
}
// Outputs: [sells, seashells, Seashore]
System.out.println(matches);

MySQL中的正則表達式

函數

函數名稱說明
REGEXP字符串是否匹配正則表達式
REGEXP_INSTR()匹配正則表達式的子字符串的起始索引
(注意:僅限 MySQL 8.0+)
REGEXP_LIKE()字符串是否匹配正則表達式
(注意:僅 MySQL 8.0+)
REGEXP_REPLACE()替換匹配正則表達式的子字符串
(注意:僅限 MySQL 8.0+)
REGEXP_SUBSTR()返回匹配正則表達式的子字符串
(注意:僅 MySQL 8.0+)

REGEXP

expr REGEXP pat 

Examples

mysql> SELECT 'abc' REGEXP '^[a-d]';
1
mysql> SELECT name FROM cities WHERE name REGEXP '^A';
mysql> SELECT name FROM cities WHERE name NOT REGEXP '^A';
mysql> SELECT name FROM cities WHERE name REGEXP 'A|B|R';
mysql> SELECT 'a' REGEXP 'A', 'a' REGEXP BINARY 'A';
1   0

REGEXP_REPLACE

REGEXP_REPLACE(expr, pat, repl[, pos[, occurrence[, match_type]]])

例子

mysql> SELECT REGEXP_REPLACE('a b c', 'b', 'X');
a X c
mysql> SELECT REGEXP_REPLACE('abc ghi', '[a-z]+', 'X', 1, 2);
abc X

REGEXP_SUBSTR

REGEXP_SUBSTR(expr, pat[, pos[, occurrence[, match_type]]])

例子

mysql> SELECT REGEXP_SUBSTR('abc def ghi', '[a-z]+');
abc
mysql> SELECT REGEXP_SUBSTR('abc def ghi', '[a-z]+', 1, 3);
ghi

REGEXP_LIKE

REGEXP_LIKE(expr, pat[, match_type])

例子

mysql> SELECT regexp_like('aba', 'b+')
1
mysql> SELECT regexp_like('aba', 'b{2}')
0
mysql> # i: case-insensitive
mysql> SELECT regexp_like('Abba', 'ABBA', 'i');
1
mysql> # m: multi-line
mysql> SELECT regexp_like('a\nb\nc', '^b$', 'm');
1

REGEXP_INSTR

REGEXP_INSTR(expr, pat[, pos[, occurrence[, return_option[, match_type]]]])

例子

mysql> SELECT regexp_instr('aa aaa aaaa', 'a{3}');
2
mysql> SELECT regexp_instr('abba', 'b{2}', 2);
2
mysql> SELECT regexp_instr('abbabba', 'b{2}', 1, 2);
5
mysql> SELECT regexp_instr('abbabba', 'b{2}', 1, 3, 1);
7

也可以看看

  • (github.com)
  • (jaywcjlove.github.io)
  • (jaywcjlove.github.io)