EsgynDB新版本之正则函数

EsgynDB2.7版本中支持三种常用的正则函数,

  • REGEXP_REPLACE
  • REGEXP_SUBSTR
  • REGEXP_COUNT

REGEXP_REPLACE

  1. 描述

根据正则表达式做字符串替换

  1. 语法

REGEXP_REPLACE(instr,pattern,replace_str)

  1. 示例
//将 01234 替换为 0abc
select regexp_replace('0123456789','01234','0abc') from dual;
SQL>select regexp_replace('0123456789','01234','0abc') from dual;

(EXPR)
--------------------
0abc56789           

--- 1 row(s) selected.

将匹配到的数字替换为’ *# 
select regexp_replace('01234abcde56789','[0-9]','*#') as new_str from dual;
SQL>select regexp_replace('01234abcde56789','[0-9]','*#') as new_str from dual;

NEW_STR
------------------------------
*#*#*#*#*#abcde*#*#*#*#*#     

--- 1 row(s) selected.

//将字符串中的字母去掉(小写字母)
select regexp_replace('01234abcde56789','[a-z]','') as new_str from dual;
SQL>select regexp_replace('01234abcde56789','[a-z]','') as new_str from dual;

NEW_STR
------------------------------
0123456789                    

--- 1 row(s) selected.

REGEXP_SUBSTR

  1. 描述
    根据正则表达式做字符串截取
  2. 语法
    REGEXP_SUBSTR(instr,pattern[,position[,occurrence]])
  3. 示例
//查询使用正则分割后的第一个值,也就是17
select regexp_substr('17,20,23','[^,]+',1,1) as str from dual;  
SQL>select regexp_substr('17,20,23','[^,]+',1,1) as str from dual; 

STR
--------
17      

--- 1 row(s) selected.

//查询使用正则分割后的最后一个值,也就是23
select regexp_substr('17,20,23','[^,]+',1,3) as str from dual;  
SQL>select regexp_substr('17,20,23','[^,]+',1,3) as str from dual;  

STR
--------
23      

--- 1 row(s) selected.

//获取一个多个数值的列,从而能够让结果以多行的形式展示出来
select nvl(regexp_substr('17,20,23', '[^,]+', 1, level), 'null') as str from dual connect by level <= 7;  
SQL>select nvl(regexp_substr('17,20,23', '[^,]+', 1, level), 'null') as str from dual connect by level <= 7;  

STR
--------
17      
20      
23      
null    
null    
null    
null    

--- 7 row(s) selected.

REGEXP_COUNT

  1. 描述
    根据正则表达式统计匹配文本出现次数
  2. 语法
    REGEXP_COUNT (instr, pattern [, position])
  3. 示例
//得到字符串中ge的出现次数 
select regexp_count('george','ge',1) from dual;
SQL>select regexp_count('george','ge',1) from dual;

(EXPR)
----------
         2

--- 1 row(s) selected.

//得到字符串中小写字母“a”的出现次数 
select regexp_count ('The pro-niece was born today, so exciting.', 'a') "Count 'a'" from dual; 

SQL>select regexp_count ('The pro-niece was born today, so exciting.', 'a') "Count 'a'" from dual; 

Count 'a'
----------
         2

--- 1 row(s) selected.

//从第17个字符处开始检索字母a
select regexp_count ('The pro-niece was born today, so exciting!', 'a', 17) "Count 'a'"  from dual; 
SQL>select regexp_count ('The pro-niece was born today, so exciting!', 'a', 17) "Count 'a'"  from dual; 

Count 'a'
----------
         1

--- 1 row(s) selected.
发布了352 篇原创文章 · 获赞 400 · 访问量 73万+

猜你喜欢

转载自blog.csdn.net/Post_Yuan/article/details/101032638