加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式

发布时间:2020-12-13 23:07:39 所属栏目:百科 来源:网络整理
导读:ASCII码表 create table ascii_tab(n number(5),s varchar2(4)); insert into ascii_tab select rownum-1,chr(rownum-1) from dual connect by rownum=129 union select 255,chr(255) from dual; commit; 将表中的s字段是数字(digit)的提取出来 SQL select

ASCII码表

create table ascii_tab(n number(5),s varchar2(4));
insert into ascii_tab select rownum-1,chr(rownum-1) from dual connect by rownum<=129 union select 255,chr(255) from dual;
commit;


将表中的s字段是数字(digit)的提取出来

SQL> select * from ascii_tab where regexp_like(s,'[[:digit:]]');
 
     N S
------ ----
    48 0
    49 1
    50 2
    51 3
    52 4
    53 5
    54 6
    55 7
    56 8
    57 9
*select * from ascii_tab where regexp_like(s,'[[:alpha:]]'); ---alpha字母

select * from ascii_tab where regexp_like(s,'[[:lower:]]'); ---小写字母

---[A-Za-z0-9]
[:alpha:] ---[A-Za-z]
[:blank:] ---[ ],标准是空格和TAB,但Oracle里只有空格!
[:cntrl:] ---[x00-x1Fx7F]
[:graph:] ---[x21-x7E],即所有可见字符
[:print:] ---[x20-x7E],即空格+所有可见字符

[:punct:] ---标点。[][!"#$%&'()*+,./:;<=>?@^_`{|}~-]

[:space:] ---[ tnvfr] ,空白字符


== select * from ascii_tab where regexp_like(s,'[^[:digit:]]'); ----取出所有非数字的字符



正则表达式的应用:

验证格式:电话号码、电子邮箱、IP地址、网址


查找替换

格式化

1、将IPV4的地址每段都格式化为三位数的,不够的前面补0

2、将字符串str逐字符重复4次,例如'abc'变为'aaaabbbbcccc' regexp_replace(str,'(.)','1111')

3、剔除str中除了逗号和数字之外的字符 regexp_replace(str,'[^[:digit:],]')


提取

1、从str中提取第一个单词(只有字母组成的才算单词)

regexp_substr(str,'([[:alpha:]]+)') ---默认从左边第一个位置提取第一个单词
regexp_substr(str,'([[:lower:]]+)',1,'i')


2、将可能是日期的字符串提取出来(yyyy-mm-dd格式)
regexp_substr(str,'d{4}-d{2}-d{2}') -- d 表示1-9的数字 、D表示非数字的字符 、 '.'代表任意字符


3、网页源文件的片段已存入str中,从中提取出第一个标签 regexp_substr(str,'<[^>]+>') ---贪婪算法 regexp_substr(str,'<.+?>') ---非贪婪算法

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读