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:]]'); ---小写字母
select * from ascii_tab where regexp_like(s,'[[:upper:]]'); ---大写字母
[:alnum:] ---[A-Za-z0-9]
[:alpha:] ---[A-Za-z]
[:blank:] ---[ ],标准是空格和TAB,但Oracle里只有空格!
[:cntrl:] ---[\x00-\x1F\x7F]
[:graph:] ---[\x21-\x7E],即所有可见字符
[:print:] ---[\x20-\x7E],即空格+所有可见字符
[:punct:] ---标点。[\]\[!"#$%&'()*+,./:;<=>?@\^_`{|}~-]
[:space:] ---[ \t\n\v\f\r] ,空白字符
select * from ascii_tab where regexp_like(s,'\D'); == select * from ascii_tab where regexp_like(s,'[^[:digit:]]'); ----取出所有非数字的字符
正则表达式的应用:
验证格式:电话号码、电子邮箱、IP地址、网址
查找替换:
格式化
1、将IPV4的地址每段都格式化为三位数的,不够的前面补0
2、将字符串str逐字符重复4次,例如'abc'变为'aaaabbbbcccc' regexp_replace(str,'(.)','\1\1\1\1')
3、剔除str中除了逗号和数字之外的字符 regexp_replace(str,'[^[:digit:],]')
提取
1、从str中提取第一个单词(只有字母组成的才算单词)
regexp_substr(str,'([[:alpha:]]+)') ---默认从左边第一个位置提取第一个单词
regexp_substr(str,'([[:lower:]]+)',1,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,'<.+?>') ---非贪婪算法