Home Database Mysql Tutorial Oracle正则表达式函数

Oracle正则表达式函数

Jun 07, 2016 pm 03:50 PM
oracle function regular expression shark

Oracle正则表达式函数 1、正则表达式中的元字符 元字符 意思 例子 / 说明要匹配的字符是一个特殊字符、常量或者后者引用。(后引用重复上一次的匹配) /n 匹配换行符 // 匹配 / /( 匹配 ( /) 匹配 ) ^ 匹配字符串的开头位置 如果A是字符串的第一个字符,^A

Oracle正则表达式函数

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作为一个包含多行的字符串。

sql 代码

 

  1. DECLARE   
  2.     V_FIRST_NAME VARCHAR2(50);  
  3.     V_DOB DATE ;  
  4. BEGIN   
  5.     --返回1965-1968你出生的FIRST_NAME以‘J’开头的消费者    
  6.     SELECT  FIRST_NAME,DOB  INTO  V_FIRST_NAME,V_DOB  
  7.     FROM  CUSTOMERS  
  8.     WHERE  REGEXP_LIKE(TO_CHAR(DOB,'YYYY'),'^196[5-8]$')  AND  REGEXP_LIKE(FIRST_NAME,'^J');  
  9.       
  10.     DBMS_OUTPUT.PUT_LINE(V_FIRST_NAME);  
  11.       
  12. END ;  



3、REGEXP_INSTR(x,pattern[,start[,occurrence[,return_option[, match_option]]]])用于在x中查找pattern。返回pattern在x中出现的位置。匹配位置从1开始。可以参考字符串函数 INSTR(),参数相关:
    'start'   开始查找的位置;
    'occurrence'   说明应该返回第几次出现pattern的位置;
    'eturn_option'   说明应该返回什么整数。若该参数为0,则说明要返回的整数是x中的一个字符的位置;若该参数为非0的整数,则说明要返回的整数为x中出现在pattern之后的字符的位置;
    'match_option'   修改默认的匹配设置。

sql 代码

 

  1. DECLARE   
  2.     V_RESULT INTEGER ;  
  3. BEGIN   
  4.     --返回17,找出l开头的 后跟4个任意字母的单词在第一个参数中第一次出现的位置,这里是light中l的位置    
  5.     SELECT  REGEXP_INSTR('But, soft! What light through yonder window breaks?','l[[:alpha:]] {4}',1,1,0)  INTO  V_RESULT  
  6.     FROM  DUAL;  
  7.       
  8.     DBMS_OUTPUT.PUT_LINE(V_RESULT);  
  9.       
  10.     --返回22,找出l开头的 后跟4个任意字母的单词在第一个参数中第一次出现的位置,这里是light中t的位置+1    
  11.     SELECT  REGEXP_INSTR('But, soft! What light through yonder window breaks?','l[[:alpha:]] {4}',1,1,1)  INTO  V_RESULT  
  12.     FROM  DUAL;  
  13.       
  14.     DBMS_OUTPUT.PUT_LINE(V_RESULT);  
  15. END ;  


4、REGEXP_REPLACE(x,pattern[,replace_string[,start[,occurrence[, match_option]]]])用于在x中查找pattern,并将其替换为replae_string。可以参考字符串函数 REPLACE(),参数同REGEXP_INSTR函数,参考第3

sql 代码

 

  1. DECLARE     
  2.     V_RESULT VARCHAR2(255);    
  3. BEGIN     
  4.     --返回But, soft! What XXX through yonder window breaks? 用‘XXX’代替‘light’    
  5.     SELECT  REGEXP_REPLACE('But, soft! What light through yonder window breaks?','l[[:alpha:]] {4}','XXX')  INTO  V_RESULT    
  6.     FROM  DUAL;          
  7.     DBMS_OUTPUT.PUT_LINE(V_RESULT);         
  8. END ;   


5、REGEXP_SUBSTR(x,pattern[,start[,occurrence[, match_option]]])用于在x中查找pattern并返回。可以参考字符串函数 SUBSTR(),参数同REGEXP_INSTR函数,参考第3

sql 代码

 

  1. DECLARE     
  2.     V_RESULT VARCHAR2(255);    
  3. BEGIN     
  4.     --返回‘light’    
  5.     SELECT  REGEXP_SUBSTR('But, soft! What light through yonder window breaks?','l[[:alpha:]] {4}')  INTO  V_RESULT    
  6.     FROM  DUAL;          
  7.     DBMS_OUTPUT.PUT_LINE(V_RESULT);         
  8. END ;    
Statement of this Website
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check tablespace size of oracle How to check tablespace size of oracle Apr 11, 2025 pm 08:15 PM

To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

How to import oracle database How to import oracle database Apr 11, 2025 pm 08:06 PM

Data import method: 1. Use the SQLLoader utility: prepare data files, create control files, and run SQLLoader; 2. Use the IMP/EXP tool: export data, import data. Tip: 1. Recommended SQL*Loader for big data sets; 2. The target table should exist and the column definition matches; 3. After importing, data integrity needs to be verified.

How to add table fields to oracle How to add table fields to oracle Apr 11, 2025 pm 07:30 PM

Use the ALTER TABLE statement, the specific syntax is as follows: ALTER TABLE table_name ADD column_name data_type [constraint-clause]. Where: table_name is the table name, column_name is the field name, data_type is the data type, and constraint-clause is an optional constraint. Example: ALTER TABLE employees ADD email VARCHAR2(100) Add an email field to the employees table.

How to create a table in oracle How to create a table in oracle Apr 11, 2025 pm 08:00 PM

Creating an Oracle table involves the following steps: Use the CREATE TABLE syntax to specify table names, column names, data types, constraints, and default values. The table name should be concise and descriptive, and should not exceed 30 characters. The column name should be descriptive, and the data type specifies the data type stored in the column. The NOT NULL constraint ensures that null values ​​are not allowed in the column, and the DEFAULT clause specifies the default values ​​for the column. PRIMARY KEY Constraints to identify the unique record of the table. FOREIGN KEY constraint specifies that the column in the table refers to the primary key in another table. See the creation of the sample table students, which contains primary keys, unique constraints, and default values.

How to solve garbled code in oracle How to solve garbled code in oracle Apr 11, 2025 pm 10:09 PM

Oracle garbled problems can be solved by checking the database character set to ensure they match the data. Set the client character set to match the database. Convert data or modify column character sets to match database character sets. Use Unicode character sets and avoid multibyte character sets. Check that the language settings of the database and client are correct.

How to uninstall Oracle installation failed How to uninstall Oracle installation failed Apr 11, 2025 pm 08:24 PM

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

How to re-query oracle How to re-query oracle Apr 11, 2025 pm 07:33 PM

Oracle provides multiple deduplication query methods: The DISTINCT keyword returns a unique value for each column. The GROUP BY clause groups the results and returns a non-repetitive value for each group. The UNIQUE keyword is used to create an index containing only unique rows, and querying the index will automatically deduplicate. The ROW_NUMBER() function assigns unique numbers and filters out results that contain only line 1. The MIN() or MAX() function returns non-repetitive values ​​of a numeric column. The INTERSECT operator returns the common values ​​of the two result sets (no duplicates).

How to encrypt oracle view How to encrypt oracle view Apr 11, 2025 pm 08:30 PM

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

See all articles