search
HomeDatabaseMysql Tutorialoracle正则表达式函数 匹配 手机

文章介绍了关于oracle正则函数的一些用法,包括匹配等,Oracle10g提供了在查询中使用正则表达的功能,它是通过各种支持正则表达式的函数在where子句中实现的

文章介绍了关于oracle正则函数的一些用法,包括匹配等,Oracle10g提供了在查询中使用正则表达的功能,它是通过各种支持正则表达式的函数在where子句中实现的

ORACLE中的支持正则表达式的函数主要有下面四个:

1,REGEXP_LIKE :与LIKE的功能相似

2,REGEXP_INSTR :与INSTR的功能相似

3,REGEXP_SUBSTR :与SUBSTR的功能相似

4,REGEXP_REPLACE :与REPLACE的功能相似


1、正则表达式中的元字符
元字符 意思 例子
说明要匹配的字符是一个特殊字符、常量或者后者引用。(后引用重复上一次的匹配) n 匹配换行符
\ 匹配
( 匹配 (
) 匹配 )
^ 匹配字符串的开头位置 如果A是字符串的第一个字符,^A 匹配 A
$ 匹配字符串的末尾位置 如果B是字符串的最后一个字符,$B 匹配 B
* 匹配前面的字符0次或多次 ba*rk可以匹配 brk、bark、baark等等
+ 匹配前面的字符1次或多次 ba+rk可以匹配 bark、baark等等,但是不能匹配brk,也就是说,最少有以一次。
? 匹配前面的字符0次或1次 ba?rk可以匹配 bark、brk等等,但是不能匹配baark。
{n} 匹配前面的字符恰好是n次,其中n是整数 hob{2}it可以匹配hobbit
{n,m} 匹配前面的字符至少是n次,最多是m次,其中n,m都是整数 hob{2,3}it可以匹配hobbit或者hobbbit
. 匹配除null以外的任意单个字符 hob.it中的.可以是任意的单个字符,如:hobsit等等
(pattern) 括号中pattern是一个子正则表达式,匹配指定pattern模式的一个子表达式。 如:aaa(x|y)可以匹配aaax或者aaay。
x|y 匹配“或” x|y可以匹配x或者y
[abc] 可以匹配abc中的任何单个字符 hello[abc]可以匹配helloa,hellob,helloc
[a-z] 可以匹配指定范围内的任何单个字符 hell[a-z]可以匹配hello或者hellz
[::] 指定一个字符类,可以匹配该类中的任何字符 [:alphanum:]可以匹配字符0-9、A-Z、a-z
[:alpha:]可以匹配字符A-Z、a-z
[:blank:]可以匹配空格或tab键
[:digit:]可以匹配数字0-9
[:graph:]可以匹配非空字符
[:lower:]可以匹配小写字母a-z
[:print:]与[:graph:]类似,不同之处在于[:print:]包括空格字符
[:punct:]可以匹配标点符号.,""等等
[:space:]可以匹配所有的空字符
[:upper:]可以匹配大写字母A-Z
[:xdigit:]可以匹配十六进制数字0-9、A-F、a-f
n 这是对前一次匹配命中的一个后引用,其中n是一个正整数 (.)1可以匹配两个连续相同的非空字符。(.)可以匹配除null以外的任何单个字符,而1则重复上一次匹配的内容,即再次匹配相同的字符,因此可以匹配两个连续相同的非空字符

2、REGEXP_LIKE(x,pattern[,match_option])用于在x中查找正则表达式pattern,该函数还可以提供一个可选的参数match_option字符串说明默认的匹配选项。match_option的取值如下:
 ‘c’   说明在进行匹配时区分大小写(缺省值);
  'i'   说明在进行匹配时不区分大小写;
  'n'   允许使用可以匹配任意字符的操作符;
  'm'   将x作为一个包含多行的字符串。

--测试数据

 代码如下 复制代码

create table test(mc varchar2(60));

insert into test values('112233445566778899');
insert into test values('22113344 5566778899');
insert into test values('33112244 5566778899');
insert into test values('44112233 5566 778899');
insert into test values('5511 2233 4466778899');
insert into test values('661122334455778899');
insert into test values('771122334455668899');
insert into test values('881122334455667799');
insert into test values('991122334455667788');
insert into test values('aabbccddee');
insert into test values('bbaaaccddee');
insert into test values('ccabbddee');
insert into test values('ddaabbccee');
insert into test values('eeaabbccdd');
insert into test values('ab123');
insert into test values('123xy');
insert into test values('007ab');
insert into test values('abcxy');
insert into test values('The final test is is is how to find duplicate words.');

commit;

一、REGEXP_LIKE

 代码如下 复制代码

select * from test where regexp_like(mc,'^a{1,3}');
select * from test where regexp_like(mc,'a{1,3}');
select * from test where regexp_like(mc,'^a.*e$');
select * from test where regexp_like(mc,'^[[:lower:]]|[[:digit:]]');
select * from test where regexp_like(mc,'^[[:lower:]]');
Select mc FROM test Where REGEXP_LIKE(mc,'[^[:digit:]]');
Select mc FROM test Where REGEXP_LIKE(mc,'^[^[:digit:]]');

二、REGEXP_INSTR

 代码如下 复制代码

Select REGEXP_INSTR(mc,'[[:digit:]]$') from test;
Select REGEXP_INSTR(mc,'[[:digit:]]+$') from test;
Select REGEXP_INSTR('The price is $400.','$[[:digit:]]+') FROM DUAL;
Select REGEXP_INSTR('onetwothree','[^[[:lower:]]]') FROM DUAL;
Select REGEXP_INSTR(',,,,,','[^,]*') FROM DUAL;
Select REGEXP_INSTR(',,,,,','[^,]') FROM DUAL;

三、REGEXP_SUBSTR

 代码如下 复制代码

SELECT REGEXP_SUBSTR(mc,'[a-z]+') FROM test;
SELECT REGEXP_SUBSTR(mc,'[0-9]+') FROM test;
SELECT REGEXP_SUBSTR('aababcde','^a.*b') FROM DUAL;

四、REGEXP_REPLACE

 代码如下 复制代码

Select REGEXP_REPLACE('Joe Smith','( ){2,}', ',') AS RX_REPLACE FROM dual;
Select REGEXP_REPLACE('aa bb cc','(.*) (.*) (.*)', '3, 2, 1') FROM dual;

SQL> select * from test;

ID MC
-------------------- ------------------------------------------------------------
A AAAAA
a aaaaa

b bbbbb

SQL> select * from test where regexp_like(id,'b','i'); --不区分数据大小写

ID MC
-------------------- ------------------------------------------------------------

b bbbbb

#End

接下来的几节将会介绍更多有关正则表达式函数的知识。

1. REGEXP_LIKE()
REGEXP_LIKE(x, pattern [, match_option])用于在x中查找pattern参数中定义的正则表达式,该函数还可以提供一个可选参数match_option,它可以设置为下面几个字符之一:

'c',说明在匹配时区分大小写(默认选项)
'I',说明在匹配时不区分大小写
'n',允许使用可以匹配任意字符的操作符
'm',将x 作为一个包含多行的字符串
下面这个查询使用REGEXP_LIKE函数检索生日在1965年到1968年之间的顾客:

 代码如下 复制代码

SELECT customer_id, first_name, last_name, dob

FROM customers

WHERE REGEXP_LIKE(TO_CHAR(dob, 'YYYY'), '^196[5-8]$');

 

CUSTOMER_ID FIRST_NAME LAST_NAME    DOB

----------- ---------- ---------- ---------

                 1 John               Brown        01-JAN-65

                 2 Cynthia            Green        05-FEB-68

下面这个查询检索名字以J或j开头的顾客。注意传递给REGEXP_LIKE()的正则表达式是 ^j,匹配选项是i,这说明不区分大小写,因此在本例中,^j 可以匹配J或j:

 代码如下 复制代码

SELECT customer_id, first_name, last_name, dob

FROM customers

WHERE REGEXP_LIKE(first_name, '^j', 'i');

 

CUSTOMER_ID FIRST_NAME LAST_NAME    DOB

----------- ---------- ---------- ---------

                 1 John               Brown        01-JAN-65

2. REGEXP_INSTR()
REGEXP_INSTR(x, pattern [, start [, occurrence [, return_option [, match_option]]]])用于在x中查找pattern;REGEXP_INSTR()返回pattern出现的位置。匹配位置从1开始。

下面这个查询使用REGEXP_INSTR函数返回匹配正则表达式 l[[:alpha:]]{4}的位置:

 代码如下 复制代码

SELECT

REGEXP_INSTR('But, soft! What light through yonder window breaks?',

'l[[:alpha:]]{4}') AS result

FROM dual;

 

RESULT

----------

17

注意返回值为17,这是light中l的位置。

下面这个查询返回第二次匹配正则表达式 s[[:alpha:]]{3}的位置,匹配位置从1开始:

 代码如下 复制代码

SELECT

REGEXP_INSTR('But, soft! What light through yonder window softly breaks?',

's[[:alpha:]]{3}', 1, 2) AS result

FROM dual;

 

RESULT

----------

45

下面这个查询使用REGEXP_INSTR函数返回第二次匹配字母 o 的位置,匹配位置从10开始:

 代码如下 复制代码

SELECT

REGEXP_INSTR('But, soft! What light through yonder window breaks?',

'o', 10, 2) AS result

FROM dual;

 

RESULT

----------

32

3. REGEXP_REPLACE()
REGEXP_REPLACE(x, pattern [, replace_string [, start [, occurrence[, match_option]]]])用于在x中查找pattern,并将其替换为 replace_string。

下面这个查询使用REGEXP_REPLACE函数将匹配正则表达式 l[[:alpha:]]{4}的子字符串替换为字符串 sound:

 代码如下 复制代码

SELECT

REGEXP_REPLACE('But, soft! What light through yonder window breaks?',

'l[[:alpha:]]{4}', 'sound') AS result

FROM dual;

 

RESULT

---------------------------------------------------

But, soft! What sound through yonder window breaks?

注意light已经被替换为sound。

4. REGEXP_SUBSTR()
REGEXP_SUBSTR(x, pattern[, start [, occurrence[, match_option]]])用于在x中查找匹配pattern的子字符串,开始位置由 start指定。

下面这个查询使用REGEXP_SUBSTR函数返回匹配正则表达式 l[[:alpha:]]{4}的子字符串:

 代码如下 复制代码

SELECT

REGEXP_SUBSTR('But, soft! What light through yonder window breaks?',

'l[[:alpha:]]{4}') AS result

FROM dual;

 

RESUL

-----

light

5. REGEXP_COUNT()
REGEXP_COUNT()是Oracle Database11g新增加的一个函数。REGEXP_COUNT(x, pattern[, start [,match_option]])用于在x中查找pattern,并返回pattern在x中出现的次数。可以提供可选参数start,指出要从x中开始查找pattern的那个字符;也可以提供可选的match_option字符串,指出匹配选项。

下面这个查询使用REGEXP_COUNT函数返回正则表达式s[[:alpha:]]{3}出现的次数:

 代码如下 复制代码

SELECT

 REGEXP_COUNT('But, soft! What light through yonder window softly breaks?',

  's[[:alpha:]]{3}') AS result

FROM dual;

 

    RESULT

----------

 2

注意返回结果是2,这表明正则表达式在提供的字符串中有两次匹配。

•oracle正则表达匹配手机特号   
尾号四连号:([0123456789])111$   如:13498212222、13613431111

sql:select * from tb_phone where REGEXP_LIKE(phone_no,'([0123456789])111$')

尾号四连顺:(0123|1234|2345|3456|4567|5678|6789)$  如:13576531234、13623432345

尾号倒四连顺:(9876|8765|7654|6543|5432|4321|3210)$  如:13512329876、13676987654

尾号00XX:00[[:digit:]][[:digit:]]$  如:13512320023、13512320035

尾号AABB:([[:digit:]])1([[:digit:]])2$  如:13567545566

尾号ABAB:([[:digit:]]{2})1$   如:13545341212

尾号AAAB:([[:digit:]])11[[:digit:]]$  如:13564326667

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools