3

我的数据库中有一组具有 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.

我是正则表达式的初学者,但根据我的研究,似乎上面应该实现我正在寻找的行为。有人能告诉我他们有什么问题吗?

4

2 回答 2

4

设置样本数据:

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 结尾的值。

于 2015-03-26T15:23:47.297 回答
0

你可以word boundary [[:>:]]在这里使用。

像这样的东西REGEXP_LIKE(col, 'tac903.2[a-z]?[[:>:]]');

见演示。

http://www.sqlfiddle.com/#!9/940fd/2/0

编辑:

`REGEXP_LIKE(col, 'tac903.2[a-z]?(\s|$|\W)');`
于 2015-03-26T15:15:10.163 回答