也许更清晰的测试是生成一些包含各种空白字符的字符串,然后使用case 表达式来查看它们是否匹配不同的正则表达式。
with demo (str) as
( select ':' from dual union all
select 'a' from dual union all
select 'b' from dual union all
select 'c' from dual union all
select 'contains'||chr(9)||'tabs' from dual union all
select 'contains'||chr(10)||chr(13)||'linebreaks' from dual union all
select 'contains some spaces' from dual
)
select str
, case when regexp_like(str,'[:blank:]') then 'Y' end as "[:blank:]"
, case when regexp_like(str,'[[:blank:]]') then 'Y' end as "[[:blank:]]"
, case when regexp_like(str,'[[:space:]]') then 'Y' end as "[[:space:]]"
, case when regexp_like(str,'\s') then 'Y' end as "\s"
from demo
order by 1;
STR [:blank:] [[:blank:]] [[:space:]] \s
-------------------- --------- ----------- ----------- --
: Y
a Y
b Y
c
contains tabs Y Y Y
contains Y Y Y
linebreaks
contains some spaces Y Y Y Y
(我手动编辑了带有标签的行的结果以对齐结果,否则标签会弄乱它并使其更难阅读。)
[:blank:] 匹配任何:、b、l、a、n、k,因为字符类仅在[] 括号表达式中有效。
[[:blank:]] 只匹配空格。
[[:space:]] 匹配制表符、换行符、回车符和空格。
\s 与[[:space:]] 相同
至于您的示例,它在两种不同的方式上都没有按照您的预期表现。
首先,[^[[:blank:]]] 应该是 [^[:blank:]] - 即括号表达式中的字符类 [:blank:]。
其次,当没有空格时,更正的语法仍然返回匹配,因为它会查找任何不是空格的字符,例如第一个字符 G 不是空格,因此它匹配表达式:
regexp_like('Greg94/Eric99Chandler/Faulkner','[^ ]');
要识别不包含任何空白字符的字符串,您应该使用:
not regexp_like(str,'\s')
或
not regexp_like(str, '[[:space:]]')