我有一个工作脚本:
Select col from table where regexp_like (col,'^noun[ |s |es ]| noun[ |s |es ]|noun[ |s |es ]$','i');我能把REGEXP中的三个块优化成一个更短的形式吗?
Good:
noun abcd
nouns abcd
abcd noun abcd
abcd nounes abcd
abcd noun
Wrong:
nounse abcd
abcd anouns abcd
abcd inoun发布于 2018-05-19 11:53:14
在大多数正则表达式引擎中,可以使用单词边界\b来获取独立的单词。
但在Oracle regex中,您需要采取不同的做法。
(^|\s)noun(e?s)?(\s|$)(^x\s):字符串或空格的开始 (e?s):有“es”或“s”的可选组 (\s=$):空格或字符串结尾
设置测试数据:
create table test_table (id number(8,0), col varchar2(30), matchexpected char(1));
insert into test_table (id, col, matchexpected) values (1,'noun abcd','Y');
insert into test_table (id, col, matchexpected) values (2,'nouns abcd','Y');
insert into test_table (id, col, matchexpected) values (3,'abcd NOUN abcd','Y');
insert into test_table (id, col, matchexpected) values (4,'abcd nounEs abcd','Y');
insert into test_table (id, col, matchexpected) values (5,'abcd noun','Y');
insert into test_table (id, col, matchexpected) values (6,'nounse abcd','N');
insert into test_table (id, col, matchexpected) values (7,'abcd anouns abcd','N');
insert into test_table (id, col, matchexpected) values (8,'abcd inoun','N');示例查询:
select *
from test_table
where regexp_like (col,'(^|\s)noun(e?s)?(\s|$)','i');或者在正则表达式中使用\W (非字字符:[^A-Za-z0-9_])。而不是\s (空格)。也可以匹配"abc nounes!“这样的字符串。
select *
from test_table
where regexp_like (col,'(^|\W)noun(e?s)?(\W|$)','i');结果:
前5个身份证。
https://stackoverflow.com/questions/50424346
复制相似问题