我的数据库中有一组具有 VARCHAR2 列值的行,如下所示:tac903.2、tac903.24、tac903.2a、tac903.2b
如果搜索表达式中的最后一个字符是数字,我想创建一个不匹配的正则表达式。因此,当我搜索“tac903.2”时,我只想匹配:tac903.2、tac903.2a 和 tac903.2b。这应该很简单,但我没有从此处的正则表达式中获得所需的行为:
REGEXP_LIKE(col, 'tac903.2[a-z]?'); //This is matching the tac903.24 record.
REGEXP_LIKE(col, 'tac903.2[^0-9]?'); //This also is matching the tac903.24 record.
我是正则表达式的初学者,但根据我的研究,上面的内容似乎应该可以实现我正在寻找的行为。有人可以告诉我他们有什么问题吗?
最佳答案
设置示例数据:
create table temp_matches (
rec varchar2(10)
);
insert into temp_matches values ('tac903.2');
insert into temp_matches values ('tac903.2a');
insert into temp_matches values ('tac903.2b');
insert into temp_matches values ('tac903.24');
insert into temp_matches values ('2');
insert into temp_matches values ('a');
查询:
select *
from temp_matches
where REGEXP_LIKE(rec, '(.)*[^0-9]$');
结果:
tac903.2a
tac903.2b
a
$ 表示行尾,所以我们只查找不以 0-9 结尾的任何值。
关于sql - Oracle REGEXP_LIKE 匹配非数字字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29282077/