1.1从字符串中截取子字符串 SELECT regexp_substr('1PSN/231_3253/ABc', '[[:alnum:]]+') FROM dual; Output: 1PSN [[:alnum:]]+ 表示匹配1个或者多个字母或数字字符。 SELECT regexp_substr('1PSN/231_3253/ABc', '[[:alnum:]]+', 1, 2) FROM dual; Output: 231 与上面一个例子相比,多了两个参数 1 表示从源字符串的第一个字符开始查找匹配 2 表示第2次匹配到的字符串(默认值是“1”,如上例) select regexp_substr('@@/231_3253/ABc','@*[[:alnum:]]+') from dual; Output: 231 @* 表示匹配0个或者多个@ [[:alnum:]]+ 表示匹配1个或者多个字母或数字字符 注意:需要注意“+”和“*”的区别 select regexp_substr('1@/231_3253/ABc','@+[[:alnum:]]*') from dual; Output: @ @+ 表示匹配1个或者多个@ [[:alnum:]]* 表示匹配0个或者多个字母或数字字符 select regexp_substr('1@/231_3253/ABc','@+[[:alnum:]]+') from dual; Output: Null @+ 表示匹配1个或者多个@ [[:alnum:]]+ 表示匹配1个或者多个字母或数字字符 select regexp_substr('@1PSN/231_3253/ABc125','[[:digit:]]+$') from dual; Output: 125 [[:digit:]]+$ 表示匹配1个或者多个数字结尾的字符 select regexp_substr('@1PSN/231_3253/ABc','[^[:digit:]]+$') from dual; Output: /ABc [^[:digit:]]+$ 表示匹配1个或者多个不是数字结尾的字符 select regexp_substr('Tom_Kyte@oracle.com','[^@]+') from dual; Output: Tom_Kyte [^@]+ 表示匹配1个或者多个不是“@”的字符 select regexp_substr('1PSN/231_3253/ABc','[[:alnum:]]*',1,2) from dual; Output: Null [[:alnum:]]* 表示匹配0个或者多个字母或者数字字符 注:因为是匹配0个或者多个,所以这里第2次匹配的是“/”(匹配了0次),而不是“231”,所以结果是“Null” 1.2匹配重复出现 查找连续2个小写字母 SELECT regexp_substr('Republicc Of Africaa', '([a-z])/1', 1, 1, 'i')
FROM dual; Output: cc ([a-z]) 表示小写字母a-z /1 表示匹配前面的字符的连续次数 1 表示从源字符串的第1个字符开始匹配 1 第1次出现符合匹配结果的字符 i 表示区分大小写 1.3其他一些匹配样式 查找网页地址信息 SELECT regexp_substr('Go to http://www.oracle.com/products and click on database', 'http://([[:alnum:]]+/.?){3,4}/?') RESULT
insert into test values('112233445566778899'); insert into test values('22113344 5566778899'); insert into test values('33112244 5566778899'); insert into test values('44112233 5566 778899'); insert into test values('5511 2233 4466778899'); insert into test values('661122334455778899'); insert into test values('771122334455668899'); insert into test values('881122334455667799'); insert into test values('991122334455667788'); insert into test values('aabbccddee'); insert into test values('bbaaaccddee'); insert into test values('ccabbddee'); insert into test values('ddaabbccee'); insert into test values('eeaabbccdd'); insert into test values('ab123'); insert into test values('123xy'); insert into test values('007ab'); insert into test values('abcxy'); insert into test values('The final test is is is how to find duplicate words.');
commit;
一、REGEXP_LIKE
select * from test where regexp_like(mc,'^a{1,3}'); select * from test where regexp_like(mc,'a{1,3}'); select * from test where regexp_like(mc,'^a.*e$'); select * from test where regexp_like(mc,'^[[:lower:]]|[[:digit:]]'); select * from test where regexp_like(mc,'^[[:lower:]]'); Select mc FROM test Where REGEXP_LIKE(mc,'[^[:digit:]]'); Select mc FROM test Where REGEXP_LIKE(mc,'^[^[:digit:]]');
二、REGEXP_INSTR
Select REGEXP_INSTR(mc,'[[:digit:]]$') from test; Select REGEXP_INSTR(mc,'[[:digit:]]+$') from test; Select REGEXP_INSTR('The price is $400.','$[[:digit:]]+') FROM DUAL; Select REGEXP_INSTR('onetwothree','[^[[:lower:]]]') FROM DUAL; Select REGEXP_INSTR(',,,,,','[^,]*') FROM DUAL; Select REGEXP_INSTR(',,,,,','[^,]') FROM DUAL;
三、REGEXP_SUBSTR
SELECT REGEXP_SUBSTR(mc,'[a-z]+') FROM test; SELECT REGEXP_SUBSTR(mc,'[0-9]+') FROM test; SELECT REGEXP_SUBSTR('aababcde','^a.*b') FROM DUAL;
四、REGEXP_REPLACE
Select REGEXP_REPLACE('Joe Smith','( ){2,}', ',') AS RX_REPLACE FROM dual; Select REGEXP_REPLACE('aa bb cc','(.*) (.*) (.*)', '3, 2, 1') FROM dual;
SQL select * from test;
ID MC -------------------- ------------------------------------------------------------ A AAAAA a aaaaa
b bbbbb
SQL select * from test where regexp_like(id,'b','i'); --不区分数据大小写
ID MC -------------------- ------------------------------------------------------------