Oracle 中的正则函数

本文详细介绍了Oracle数据库中五个常用的正则表达式函数:REGEXP_COUNT、REGEXP_INSTR、REGEXP_LIKE、REGEXP_REPLACE 和 REGEXP_SUBSTR 的使用方法及应用场景。通过具体示例展示了如何利用这些函数进行文本数据的统计、查询、替换和截取操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个是函数中将会用到的模式串的介绍:

 

Oracle 中的正则函数一共有5个:

REGEXP_COUNT -- 统计子串出现的次数

REGEXP_INSTR -- 查找子串在母串中的位置

REGEXP_LIKE -- 模糊查询子串

REGEXP_REPLACE -- 替换

REGEXP_SUBSTR -- 截取

 

以下是官方关于这5个函数可能会用到的参数的介绍:

  • source_char is a character expression that serves as the search value. It is commonly a character column and can be of any of the datatypes CHARVARCHAR2NCHARNVARCHAR2CLOB, or NCLOB.


  • pattern is the regular expression. It is usually a text literal and can be of any of the datatypes CHARVARCHAR2NCHAR, or NVARCHAR2. It can contain up to 512 bytes. If the datatype of pattern is different from the datatype of source_char, Oracle Database converts pattern to the datatype of source_char. For a listing of the operators you can specify in pattern, please refer to Appendix C, "Oracle Regular Expression Support".


  • position is a positive integer indicating the character of source_char where Oracle should begin the search. The default is 1, meaning that Oracle begins the search at the first character of source_char.


  • occurrence is a positive integer indicating which occurrence of pattern in source_char Oracle should search for. The default is 1, meaning that Oracle searches for the first occurrence of pattern.


  • return_option lets you specify what Oracle should return in relation to the occurrence:

    • If you specify 0, then Oracle returns the position of the first character of the occurrence. This is the default.

    • If you specify 1, then Oracle returns the position of the character following the occurrence.

  • match_parameter is a text literal that lets you change the default matching behavior of the function. You can specify one or more of the following values for match_parameter:

    • 'i' specifies case-insensitive matching.

    • 'c' specifies case-sensitive matching.

    • 'n' allows the period (.), which is the match-any-character character, to match the newline character. If you omit this parameter, the period does not match the newline character.

    • 'm' treats the source string as multiple lines. Oracle interprets ^ and $ as the start and end, respectively, of any line anywhere in the source string, rather than only at the start or end of the entire source string. If you omit this parameter, Oracle treats the source string as a single line.

    • 'x' ignores whitespace characters. By default, whitespace characters match themselves.

1. REGEXP_COUNT(<source_string>, <pattern>[[, <start_position>], [<match_parameter>]])

Sql代码   收藏代码
  1. -- 从'123123123123123'串中查找'123'子串的个数,起始查找位置为1  
  2. select regexp_count('123123123123123''123', 1, 'i'from dual;  
  3. → 5  
 

2. REGEXP_INSTR(<source_string>, <pattern>

   [[, <start_position>][, <occurrence>][, <return_option>][, <match_parameter>][,<sub_expression>]])

Sql代码   收藏代码
  1. -- 从'500 Oracle Parkway, Redwood Shores, CA'串中查找 至少包含一个非空格字符的子串的位置  
  2. -- 起始查找位置为1,查找匹配的第6个子串的位置  
  3. select regexp_instr('500 Oracle Parkway, Redwood Shores, CA''[^ ]+', 1, 6) from dual;  
  4. → 37  
  5.   
  6. -- 从'500 Oracle Parkway, Redwood Shores, CA'串中查找  
  7. -- 包含s或r或p字符,后面跟着6个字母的子串,不区分大小写('Parkway','Redwood'匹配)  
  8. -- 起始查找位置为3,查找匹配的第2个子串('Redwood'匹配)  
  9. -- 返回的位置是紧接着匹配子串后面的字符的位置('Redwood'后面空格的位置)  
  10. select regexp_instr('500 Oracle Parkway, Redwood Shores, CA',  
  11.                     '[s|r|p][[:alpha:]]{6}',  
  12.                     3,  
  13.                     2,  
  14.                     1,  
  15.                     'i')  
  16.   from dual;  
  17. → 28  
   

3. REGEXP_LIKE(<source_string>, <pattern>, <match_parameter>)

Sql代码   收藏代码
  1. -- 从指定列中查询出含有2个连续空格的字段  
  2. select source_string  
  3.   from (select 'hello, one space!' as source_string from dual  
  4.         union all  
  5.         select 'hello, two space! ' as source_string from dual  
  6.         union all  
  7.         select 'hello, two sequential space!  ' as source_string from dual)  
  8.  where regexp_like(source_string, '[[:space:]]{2}');  
  9.   
  10. -- 从指定列中查询出first_name列以'Ste'开头,'en'结尾,中间包含字母'v'或'ph'的字段  
  11. select first_name, last_name  
  12.   from (select 'Steven' as first_name, 'King' as last_name from dual  
  13.         union all  
  14.         select 'Steven' as first_name, 'Markle' as last_name from dual  
  15.         union all  
  16.         select 'Stephen' as first_name, 'Stiles' as last_name from dual)  
  17.  where regexp_like(first_name, '^Ste(v|ph)en$')  
  18.  order by first_name, last_name;  
  

4. REGEXP_REPLACE(<source_string>, <pattern>, <replace_string>

   [, <position>[, <occurrence>[, <match_parameter>]]])

Sql代码   收藏代码
  1. -- 将字符串中连续的多个空格替换成一个空格  
  2. select regexp_replace('500   Oracle     Parkway,    Redwood  Shores, CA',  
  3.                       '( ){2,}',  
  4.                       ' ')  
  5.   from dual;  
  6.   
  7. -- 把'xxx.xxx.xxxx'格式的电话号码转换为'(xxx) xxx-xxxx'格式  
  8. with T1 as(  
  9.      select '515.123.4567' as phone_number from dual  
  10.      union all  
  11.      select '515.123.4568' as phone_number from dual  
  12.      union all  
  13.      select '(515) 123 4569' as phone_number from dual  
  14.      union all  
  15.      select '13207730591' as phone_number from dual)  
  16. select regexp_replace(phone_number,  
  17.                       '([[:digit:]]{3})\.([[:digit:]]{3})\.([[:digit:]]{4})',  
  18.                       '(\1) \2-\3')  
  19.   from T1;  
  20.   
  21. -- 给字段的每个字符后面加上空格  
  22. with T2 as(  
  23.      select 'Argentina' as country_name from dual  
  24.      union all  
  25.      select 'Australia' as country_name from dual  
  26.      union all  
  27.      select 'Belgium' as country_name from dual  
  28.      union all  
  29.      select 'Brazil' as country_name from dual  
  30.      union all  
  31.      select 'Canada' as country_name from dual)  
  32. select regexp_replace(country_name,  
  33.                       '(.)',  
  34.                       '\1 ')  
  35.   from T2;  
 

5. REGEXP_SUBSTR(<source_string>, <pattern>[, <position> [, <occurrence>[, <match_parameter>]]])

Sql代码   收藏代码
  1. -- 截取字符串中两个','之间的子串,子串只能开头和结尾含有',',中间至少要含有一个非','字符  
  2. select regexp_substr('500 Oracle Parkway, Redwood Shores, CA'',[^,]+,')  
  3.   from dual;  
  4.   
  5. -- 截取字符串中包含 'http://' 子串,后面跟着3-4个 '字母/数字 0-1个.',再跟着0-1个 '/' 的子串  
  6. select regexp_substr('http://www.oracle.com/products',  
  7.                      'http://([[:alnum:]]+\.?){3,4}/?')  
  8.   from dual;  
  9.     
  10. with T3 as(select '1:3,4:6,8:10,3:4,7:6,11:12' as source_string from dual)  
  11. -- 从source_string列中查找不含':'的子串,起始查找位置为1,返回第1个子串  
  12. select regexp_substr(source_string,'[^:]+', 1, 1) from T3  
  13. union all  
  14. -- 从source_string列中查找不含':'的子串,起始查找位置为3,返回第2个子串  
  15. select regexp_substr(source_string,'[^:]+', 3, 2) from T3;  
基于Spring Boot搭建的一个多功能在线学习系统的实现细节。系统分为管理员和用户两个主要模块。管理员负责视频、文件和文章资料的管理以及系统运营维护;用户则可以进行视频播放、资料下载、参与学习论坛并享受个性化学习服务。文中重点探讨了文件下载的安全性和性能优化(如使用Resource对象避免内存溢出),积分排行榜的高效实现(采用Redis Sorted Set结构),敏感词过滤机制(利用DFA算法构建内存过滤树)以及视频播放的浏览器兼容性解决方案(通过FFmpeg调整MOOV原子位置)。此外,还提到了权限管理方面自定义动态加载器的应用,提高了系统的灵活性和易用性。 适合人群:对Spring Boot有一定了解,希望深入理解其实际应用的技术人员,尤其是从事在线教育平台开发的相关从业者。 使用场景及目标:适用于需要快速搭建稳定高效的在线学习平台的企业或团队。目标在于提供一套完整的解决方案,涵盖从资源管理到用户体验优化等多个方面,帮助开发者更好地理解和掌握Spring Boot框架的实际运用技巧。 其他说明:文中不仅提供了具体的代码示例和技术思路,还分享了许多实践经验教训,对于提高项目质量有着重要的指导意义。同时强调了安全性、性能优化等方面的重要性,确保系统能够应对大规模用户的并发访问需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值