带 "or" 运算符的 DB2 SQL 正则表达式
DB2 SQL regular expression with "or" operator
为什么这个正则表达式不起作用!?!?!?
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
始终为假,即使 A
或 B
为真。
PS:我正在使用 DB2 10
我通过多次调用 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
可能是字符编码。例如。如果 |
不是真正的 |
,而是看起来相同或相似的东西,比如说 ¦
。在这种情况下,正则表达式会尝试逐字匹配 a¦b
而不是 a
或 b
.
为什么这个正则表达式不起作用!?!?!?
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
始终为假,即使 A
或 B
为真。
PS:我正在使用 DB2 10
我通过多次调用 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
可能是字符编码。例如。如果 |
不是真正的 |
,而是看起来相同或相似的东西,比如说 ¦
。在这种情况下,正则表达式会尝试逐字匹配 a¦b
而不是 a
或 b
.