设为首页 收藏本站
查看: 678|回复: 0

[经验分享] oracle 10g支持正则表达式

[复制链接]

尚未签到

发表于 2016-7-24 11:23:32 | 显示全部楼层 |阅读模式
  

  近期调整了表结构,令 site_id 为 number(9),测试过程中发现有些包含有
  i_site_id INTEGER(2);
  这样的声明,导致 site_id 大于两位时报错
  而用 plsql developer 的 find data objects
的正则表达式功能无法忽略大小写,因此直接从源中查
  SELECT t.name, t.line, t.text
  FROM
user_source t
 WHERE
-- 全部类型的源码
-- TYPE IN
("PROCEDURE","PACKAGE","PACKAGE BODY","TRIGGER","FUNCTION","TYPE") AND 

 regexp_like(t.text, "(number|integer)\([1-8]\)", "i")
 ORDER BY t.name,
t.line;

  解释:(number|integer)\([1-8]\)
匹配:number(1~8) 这样的源码,"i"
代表忽略大小写
  
  

  ORACLE终于在10G中提供了对正则表达式的支持,以前那些需要通过LIKE来进行的复杂的匹配就可以通过使用正则表达式更简单的实现。
  ORACLE中的支持正则表达式的函数主要有下面四个:
  
1,REGEXP_LIKE :与LIKE的功能相似
  2REGEXP_INSTR :与INSTR的功能相似
  3REGEXP_SUBSTR :与SUBSTR的功能相似
  4REGEXP_REPLACE :与REPLACE的功能相似
  在新的函数中使用正则表达式来代替通配符‘%’和‘_’。
  正则表达式由标准的元字符(metacharacters)所构成:
  '^'
匹配输入字符串的开始位置,在方括号表达式中使用,此时它表示不接受该字符集合。
  '$'
匹配输入字符串的结尾位置。如果设置了
RegExp
对象的
Multiline
属性,则 $
也匹配 'n'

'r'

  '.'
匹配除换行符
n
之外的任何单字符。
  '?'
匹配前面的子表达式零次或一次。
  '+'
匹配前面的子表达式一次或多次。
  '*'
匹配前面的子表达式零次或多次。
  '|' 指明两项之间的一个选择。例子'^([a-z]+|[0-9]+)$'表示所有小写字母或数字组合成的字符串。
  '( )' 标记一个子表达式的开始和结束位置。
  '[]' 标记一个中括号表达式。
  '{m,n}' 一个精确地出现次数范围,m=<出现次数<=n'{m}'表示出现m次,'{m,}'表示至少出现m次。
  num 匹配
num
,其中 num
是一个正整数。对所获取的匹配的引用。
  字符簇:

  [[:alpha:]] 任何字母。
  [[:digit:]] 任何数字。
  [[:alnum:]] 任何字母和数字。
  [[:space:]] 任何白字符。
  [[:upper:]] 任何大写字母。
  [[:lower:]] 任何小写字母。
  [[:punct:]] 任何标点符号。
  [[:xdigit:]] 任何16进制的数字,相当于[0-9a-fA-F]
  各种操作符的运算优先级
  转义符
  (), (?:), (?=), [] 圆括号和方括号
  *, +, ?, {n}, {n,}, {n,m} 限定符
  ^, $, anymetacharacter 位置和顺序
  | “操作
  下面通过几个例子来具体说明这几个新函数的使用方法:
  SQL> create table sunwg (id varchar2(100));
  Table created.
  SQL> insert into sunwg values ('<a
href="http://sunwgneuqsoft.itpub.net/post/34741/447698">常见SQL访问索引的方式</a>');
  1 row created.
  SQL> commit;
  Commit complete.
  SQL> select * from sunwg;
  ID
  ----------------------------------------------------------------------------------------------------
  <a
href="http://sunwgneuqsoft.itpub.net/post/34741/447698">常见SQL访问索引的方式</a>
1,
REGEXP_LIKE
  REGEXP_LIKELIKE类似,用REGEXP_LIKE能实现的操作大部分都可以用LIKE实现,不过要简单方便得多。
  <a>目标:查询表sunwg中是否存在类似与3XX41的记录?
  LIKE
  select * from sunwg where id like '%3__41%';
  REGEXP_LIKE
  select * from sunwg where
regexp_like(id,'3..41');
  <b>目标:查询表sunwg中是否存在类似与3XX41的记录,并且XX必须是数字?
  LIKE
  这个LIKE我就想出来很好的实现办法了,唯一想到就是截取出来后判断该字符串是不是纯数字的。
  REGEXP_LIKE
  select * from sunwg where
regexp_like(id,'3[0-9]{2}41');
  用REGEXP_LIKE则可以简单快捷的得到结果。其他几个函数也都有类似的情况,下面的函数就不具体比较差异了,仅仅给出常用的用法。
2,
REGEXP_INSTR
  <a>目标:查询表sunwg中是否存在类似与3XX41的字符串第一次出现的位置?
  SQL> select regexp_instr(id,'3..41',1,1) from
sunwg;
  REGEXP_INSTR(ID,'3..41',1,1)
  ----------------------------
  46
  SQL> select substr(id,46,5) from sunwg;
  SUBST
  -----
  34741
3,
REGEXP_SUBSTR
  <a>目标:截取出表sunwg中的URL地址?
  SQL> select regexp_substr(id,'http[0-9a-zA-Z/:.]+') from
sunwg;
  REGEXP_SUBSTR(ID,'HTTP[0-9A-ZA-Z/:.]+')
  ----------------------------------------------------------------------------------------------------
  http://sunwgneuqsoft.itpub.net/post/34741/447698
4,
REGEXP_REPLACE
  <a>目标:替换表sunwg中的URL的地址为www.163.com?
  SQL> select
regexp_replace(id,'http[0-9a-zA-Z/:.]+','www.163.com') from sunwg;
  REGEXP_REPLACE(ID,'HTTP[0-9A-ZA-Z/:.]+','WWW.163.COM')
  ------------------------------------------------------------------------------------------------------------------------------------------------------
  <a href="www.163.com">常见SQL访问索引的方式</a>
  从上面的例子可以看得出来这几个支持正则表达式的函数是十分强大的,合理的加以使用一定会使你写出的SQL更加简单高效。 

regexp_substr







  regexp_substr (string, pattern, position)
regexp_substr (string, pattern, position, occurence)
regexp_substr (string, pattern, position, occurence, parameters)


  
parameters
can be a combination of
regexp_substr (string, pattern)




  • i: to match case insensitively

  • c: to match case sensitively

  • n: to make the dot (.) match new lines as well

  • m: to make ^ and $ match beginning and end of a line in a
    multiline string



regexp_substr is an Oracle SQL function that enables regular
expressions
in queries. It enhances the 'traditional' substr.

Links

See also On
splitting a string into words with regular expressions
where a
function uses regexp_substr to split a string.

Then there is also safe_to_number() where regexp_substr is used to convert
strings to numbers.

regexp_instr
regexp_instr
(string, pattern)
regexp_instr (string, pattern, position)
regexp_instr (string, pattern, position, occurence)
regexp_instr (string, pattern, position, occurence, return-option)
regexp_instr (string, pattern, position, occurence, return-option, parameters)



Parameters

parameters can be a combination of


  • i: to match case insensitively

  • c: to match case sensitively

  • n: to make the dot (.) match new lines as well

  • m: to make ^ and $ match beginning and end of a line in a
    multiline string

  • x: to ignore white spaces.



regexp_instr is an Oracle SQL function that enables regular
expressions
in queries. It enhances the 'traditional' instr.

regexp_like
regexp_like
(string, pattern);
regexp_like (string, pattern, parameters);



parameters can be a combination of


  • i: to match case insensitively

  • c: to match case sensitively

  • n: to make the dot (.) match new lines as well

  • m: to make ^ and $ match beginning and end of a line in a
    multiline string



regexp_like is an Oracle SQL function that enables regular
expressions
in queries. It enhances the «traditional» like.

regexp_like is a pattern condition.

Demonstration

create table strings (
str  varchar2(30)
);
create table patterns (
pat  varchar2(50),
dsc  varchar2(30)
);



insert into patterns values ('^[[:digit:]]{3}-[[:digit:]]{2}-[[:digit:]]{4}


insert into strings values ('987-65-4321');
insert into strings values ('hello foo bar');
insert into strings values ('4987-65-4321');
insert into strings values ('hello FOO BAR');
insert into strings values ('-4.55');
insert into strings values ('987-65-43213');
insert into strings values ('4.55');
insert into strings values ('hello bar bar');
insert into strings values (' 4.55');
insert into strings values ('1234567890');
insert into strings values ('hello FOO FOO');



select
str,dsc
from
strings cross join patterns
where
regexp_like(str, pat) ;



STR                            DSC
------------------------------ ------------------------------
987-65-4321                    Social security number
hello bar bar                  Repeated words
hello FOO FOO                  Repeated words
hello foo bar                  Only lowercase words
hello bar bar                  Only lowercase words
1234567890                     Only digits
987-65-4321                    At least one digit
4987-65-4321                   At least one digit
-4.55                          At least one digit
987-65-43213                   At least one digit
4.55                           At least one digit
4.55                          At least one digit
1234567890                     At least one digit
-4.55                          Number
4.55                           Number
1234567890                     Number





regexp_replace
regexp_replace
(string, pattern)
regexp_replace (string, pattern, replace-string)
regexp_replace (string, pattern, replace-string, position)
regexp_replace (string, pattern, replace-string, position, occurence)
regexp_replace (string, pattern, replace-string, position, occurence, parameters)



parameters can be a combination of


  • i: to match case insensitively

  • c: to match case sensitively

  • n: to make the dot (.) match new lines as well

  • m: to make ^ and $ match beginning and end of a line in a
    multiline string



regexp_substr is an Oracle SQL function that enables regular
expressions
in queries. It enhances the 'traditional' substr.

regexp_replace is an Oracle SQL function that enables regular
expressions
in queries. It enhances the 'traditional' replace.

Demonstration

create table strings (
str varchar2(30)
);
create table patterns (
pat  varchar2(60),
repl varchar2(30),
dsc  varchar2(30)
);



insert into patterns values ('^[[:space:]]*[^[:space:]]+[[:space:]]+([^[:space:]]+).*', '\1', 'The 2nd word');
insert into patterns values ('^[^[:digit:]]*([[:digit:]]*\.?[[:digit:]]+).*'          , '\1', 'The 1st number');
insert into patterns values ('^[^[:upper:]]*([[:upper:]]+).*'                         , '\1', 'Uppercase word');



insert into strings values ('foo  bar   baz');
insert into strings values ('bla   MOO   82.22    7.34  bla');
insert into strings values ('  one   two 3 four  ');



column found format a20
select
str,
regexp_replace(str, pat, repl) found,
dsc
from
strings cross join patterns
where
regexp_instr(str,pat) > 0;



STR                            FOUND                DSC
------------------------------ -------------------- --------------------
foo  bar   baz                 bar                  The 2nd word
bla   MOO   82.22    7.34  bla MOO                  The 2nd word
one   two 3 four             two                  The 2nd word
bla   MOO   82.22    7.34  bla 82.22                The 1st number
one   two 3 four             3                    The 1st number
bla   MOO   82.22    7.34  bla MOO                  Uppercase word



Links

See also On using
regexp_replace to format data
.





, 'Social security
number'); insert into patterns values ('[^[:alpha:]]([[:alpha:]]+)[^[:alpha:]]
*\1' , 'Repeated words'); insert into patterns values ('^([[:lower:]]| )*

___FCKpd___5


___FCKpd___6


___FCKpd___7


___FCKpd___8


parameters can be a combination of


  • i: to match case insensitively

  • c: to match case sensitively

  • n: to make the dot (.) match new lines as well

  • m: to make ^ and $ match beginning and end of a line in a
    multiline string



regexp_substr is an Oracle SQL function that enables regular
expressions
in queries. It enhances the 'traditional' substr.

regexp_replace is an Oracle SQL function that enables regular
expressions
in queries. It enhances the 'traditional' replace.

Demonstration

___FCKpd___9


___FCKpd___10


___FCKpd___11


___FCKpd___12


___FCKpd___13


Links

See also On using
regexp_replace to format data

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-248580-1-1.html 上篇帖子: oracle热备份和冷备份 下篇帖子: Oracle从表中随机抽取记录[sql]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表