MySQL学习笔记之正则表达式

Regular Expressions
SELECT prod_name
FROM products
WHERE prod_name REGEXP '1000'
ORDER BY prod_name;
# Retriveves all rows where column prod_name contains the text 1000

LIKE matches the whole colunm value while REGEXP matches within column values.

SELECT prod_name
FROM products
WHERE prod_name REGEXP '.000'
ORDER BY prod_name;

SELECT prod_name
FROM products
WHERE prod_name REGEXP '1000|2000'
ORDER BY prod_name; 

Matches are NOT case-sensitive.

SELECT prod_name
FROM products
WHERE prod_name REGEXP '[123] Ton'
ORDER BY prod_name

SELECT prod_name
FROM products
WHERE prod_name REGEXP '[1-5] Ton'
ORDER BY prod_name;

SELECT prod_name
FROM products
WHERE prod_name REGEXP '[^2-4] ton';

SELECT vend_name
FROM vendors
WHERE vend_name REGEXP '\\.';
# Attach \\ when matches special characters
MetacharacterDescription
\\fForm feed
\\nLine feed
\\rCarrige return
\\tTab
\\vVertical tab
\\\\

Character Classes

ClassDescription
[:alnum:]Any letter or digit
[:alpha:]Any letter
[:blank:]Space or tab
[:cntrl:]ASCII control characters ASCII 0-31, 127
[:digit:]Any digit
[:print:]Any printable character
[:graph:]Same as [:print:] but excludes space
[:lower:] [:upper:]
[:space:]
[:xdigit:]Any hexadecimal digit

Repetition Metacharacters

MetacharacterDescription
*0 or more matches
+1 or more mathces
?0 or 1 mathces
{n}n mathces
{n,}no less than n of matches
{n,m}range of matches(m <= 255)
SELECT prod_name
FROM products
WHERE prod_name REGEXP '\\([:digit:] sticks?\\)'
ORDER BY prod_name;
SELECT prod_name
FROM products
WHERE prod_name REGEXP '[:digit:]{4}'
ORDER BY prod_name;

Anchors

MetacharacterDescription
^Start of text
$End of text
[[:<:]]Start of word
[[:>:]]End of word

Find all products started with a number(includes .)

SELECT prod_name
FROM products
WHERE prod_name REGEXP '^[0-9\\.]'
ORDER BY prod_name;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值