带有“或”运算符的DB2 SQL正则表达式

时间:2015-11-12 19:10:15

标签: sql regex db2

为什么这个正则表达式不起作用!?!?!?

create table SAMPLE (STR char(3));    
insert into SAMPLE values ('aaa');
insert into SAMPLE values ('bbb');
insert into SAMPLE values ('ccc');

select 
    STR
    , xmlquery('fn:matches($STR,"a")') as A
    , xmlquery('fn:matches($STR,"b")') as B
    , xmlquery('fn:matches($STR,"a|b")') as A_OR_B
from SAMPLE;

结果:

STR;   A;     B;     A_OR_B
'aaa'; true;  false; false
'bbb'; false; true;  false
'ccc'; false; false; false

A_OR_B始终为false,即使AB为真。

PS:我正在使用DB2 10

2 个答案:

答案 0 :(得分:1)

通过多次调用RegExp并将它们与or运算符组合,我找到了一个狡猾的工作。

select 
    xmlquery('fn:matches($COL_NAME,"a")') as A
    , xmlquery('fn:matches($COL_NAME,"b")') as B
    , xmlquery('fn:matches($COL_NAME,"a|b")') as A_OR_B_Error
    , xmlquery('fn:matches($COL_NAME,"a") or fn:matches($COL_NAME,"b")') as A_OR_B
from MYTABLE

答案 1 :(得分:1)

可能是字符编码。例如。如果|实际上不是|,但看起来相同或类似,请说¦。在这种情况下,正则表达式尝试按字面匹配a¦b而不是ab