日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

SQL 基础正则表达式(二十三)

發(fā)布時間:2025/3/21 数据库 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQL 基础正则表达式(二十三) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在SQL 和 PL/SQL 中使用正則表達(dá)式

函數(shù)名稱描述
REGEXP_LIKE與LIKE運(yùn)算符類似,但執(zhí)行正則表達(dá)式匹配,而不是簡單的模糊匹配(條件)
REGEXP_REPLACE以正則表達(dá)式搜索和替換字符串
REGEXP_INSTR以正則表達(dá)式搜索字符串,并返回匹配的位置
REGEXP_SUBSTR以正則表達(dá)式搜索和提取匹配字符串
REGEXP_COUNT返回匹配的次數(shù)


什么是元字符?

元字符是特殊字符有特殊的含義,如一個通配符,重復(fù)字符,一個不匹配的字符,一個范圍內(nèi)的符。

您可以使用多個預(yù)定義的元字符符號的模式匹配。

例如, ^(f|ht)tps?:$ 正則表達(dá)式搜索字符串從以下開始:

– 字面值 f 或 ht

– 字面值 t

– 字面值 p,字面值s 可選

– 冒號“:” 結(jié)尾的字面值


正則表達(dá)式的元字符

語法描述
.Matches any character in the supported character set, except NULL
+Matches one or more occurrences
?Matches zero or one occurrence
*Matches zero or more occurrences of the preceding subexpression
{m}Matches exactly m occurrences of the preceding expression
{m, }Matches at least m occurrences of the preceding subexpression
{m,n}Matches at least m, but not more than n, occurrences of the preceding

subexpression

[…]Matches any single character in the list within the brackets
|Matches one of the alternatives
( ... )Treats the enclosed expression within the parentheses as a unit. The

subexpression can be a string of literals or a complex expression containing

operators.

^Matches the beginning of a string
$Matches the end of a string
\Treats the subsequent metacharacter in the expression as a literal
\nMatches the nth (1–9) preceding subexpression of whatever is grouped

within parentheses. The parentheses cause an expression to be

remembered; a backreference refers to it.

\dA digit character
[:class:]Matches any character belonging to the specified POSIX character class
[^:class:]Matches any single character not in the list within the brackets


REGEXP_LIKE (source_char, pattern [,match_option]


REGEXP_INSTR (source_char, pattern [, position

[, occurrence [, return_option

[, match_option [, subexpr]]]]])


REGEXP_SUBSTR (source_char, pattern [, position

[, occurrence [, match_option

[, subexpr]]]])


REGEXP_REPLACE(source_char, pattern [,replacestr

[, position [, occurrence

[, match_option]]]])


REGEXP_COUNT (source_char, pattern [, position

[, occurrence [, match_option]]])


使用REGEXP_LIKE 執(zhí)行基本搜索

REGEXP_LIKE(source_char, pattern [, match_parameter ])


SELECT first_name, last_name FROM employees

WHERE REGEXP_LIKE (first_name, '^Ste(v|ph)en$');


使用REGEXP_REPLACE 替換

REGEXP_REPLACE(source_char, pattern [,replacestr

[, position [, occurrence [, match_option]]]])


SELECT REGEXP_REPLACE(phone_number, '\.','-') AS phone

FROM employees;


使用 REGEXP_INSTR 插入

REGEXP_INSTR (source_char, pattern [, position [,

occurrence [, return_option [, match_option]]]])


SELECT street_address,REGEXP_INSTR(street_address,'[[:alpha:]]') AS

First_Alpha_Position

FROM locations;


使用 REGEXP_SUBSTR?函數(shù)提取字符串

REGEXP_SUBSTR (source_char, pattern [, position [, occurrence [, match_option]]])


SELECT REGEXP_SUBSTR(street_address , ' [^ ]+ ') AS Road FROM locations;


子表達(dá)式


使用子表達(dá)式與正則表達(dá)式支持

SELECT

REGEXP_INSTR

('0123456789', -- source char or search value

'(123)(4(56)(78))', -- regular expression patterns

1, -- position to start searching

1, -- occurrence

0, -- return option

'i', -- match option (case insensitive)

1) -- sub-expression on which to search

"Position"

FROM dual;


為什么要訪問第n個子表達(dá)式

一個更實(shí)際的用途:DNA測序

您可能需要找到一個特定的子模式,確定了在小鼠DNA免疫

所需的蛋白質(zhì)。


SELECT REGEXP_INSTR(' ccacctttccctccactcctcacgttctcacctgtaaagcgtccctc

cctcatccccatgcccccttaccctgcagggtagagtaggctagaaaccagagagctccaagc

tccatctgtggagaggtgccatccttgggctgcagagagaggagaatttgccccaaagctgcc

tgcagagcttcaccacccttagtctcacaaagccttgagttcatagcatttcttgagttttca

ccctgcccagcaggacactgcagcacccaaagggcttcccaggagtagggttgccctcaagag

gctcttgggtctgatggccacatcctggaattgttttcaagttgatggtcacagccctgaggc

atgtaggggcgtggggatgcgctctgctctgctctcctctcctgaacccctgaaccctctggc

taccccagagcacttagagccag ',

'(gtc(tcac)(aaag))',

1, 1, 0, 'i',

1) "Position"

FROM dual;


REGEXP_SUBSTR 示例

SELECT

REGEXP_SUBSTR

('acgctgcactgca', -- source char or search value

'acg(.*)gca', -- regular expression pattern

1, -- position to start searching

1, -- occurrence

'i', -- match option (case insensitive)

1) -- sub-expression

"Value"

FROM dual;


使用 REGEXP_COUNT函數(shù)

REGEXP_COUNT (source_char, pattern [, position

[, occurrence [, match_option]]])


SELECT REGEXP_COUNT(

'ccacctttccctccactcctcacgttctcacctgtaaagcgtccctccctcatccccatgcccccttaccctgcag

ggtagagtaggctagaaaccagagagctccaagctccatctgtggagaggtgccatccttgggctgcagagagaggag

aatttgccccaaagctgcctgcagagcttcaccacccttagtctcacaaagccttgagttcatagcatttcttgagtt

ttcaccctgcccagcaggacactgcagcacccaaagggcttcccaggagtagggttgccctcaagaggctcttgggtc

tgatggccacatcctggaattgttttcaagttgatggtcacagccctgaggcatgtaggggcgtggggatgcgctctg

ctctgctctcctctcctgaacccctgaaccctctggctaccccagagcacttagagccag' ,

'gtc') AS Count

FROM dual;


Check約束和正則表達(dá)式:示例

ALTER TABLE emp8

ADD CONSTRAINT email_addr

CHECK(REGEXP_LIKE(email,'@')) NOVALIDATE;





本文轉(zhuǎn)自 yuri_cto 51CTO博客,原文鏈接:http://blog.51cto.com/laobaiv1/1910840,如需轉(zhuǎn)載請自行聯(lián)系原作者

總結(jié)

以上是生活随笔為你收集整理的SQL 基础正则表达式(二十三)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。