正则表达式没有空间

正则表达式[[:blank:]]和\ s相同。 下面显示了2个不同的结果。

select regexp_replace('Greg94/Eric99Chandler/Faulkner','/','') 
from dual 
where regexp_like(trim('Greg94/Eric99Chandler/Faulkner'),'[^[[:blank:]]]');  

上面的查询不返回任何行,而当我用[^ / s]替换空白时,它将返回该行。

wqh467212692 回答:正则表达式没有空间

那将是

SQL> SELECT regexp_replace('Greg94/Eric99Chandler/Faulkner','/','') as result
  2  FROM dual
  3  WHERE REGEXP_LIKE(TRIM('Greg94/Eric99Chandler/Faulkner'),'[^[:blank:]]');

RESULT
--------------------------------------------------
Greg94Eric99ChandlerFaulkner

SQL>
SQL> SELECT regexp_replace('Greg94/Eric99Chandler/Faulkner','') as result
  2  FROM dual
  3  WHERE NOT REGEXP_LIKE(TRIM('Greg94/Eric99Chandler/Faulkner'),'[[:blank:]]');

RESULT
--------------------------------------------------
Greg94Eric99ChandlerFaulkner

SQL>
SQL> SELECT regexp_replace('Greg94/Eric99Chandler/Faulkner','[^\s]');

RESULT
--------------------------------------------------
Greg94Eric99ChandlerFaulkner

SQL>

选择最喜欢的那个。此外,如果您发现可以正常使用的软件,为什么不简单地使用它(而忘记了一个无效的软件)呢? (我想我知道-由于而是为什么?)。

,

问题是您使用的是[[:blank:]]而不是[:blank:]。 正则表达式[^ [[:: blank:]]]计算:

  1. ^ [[[:blank:]]:列表“ [,[:blank:]””中没有字符
  2. ]要评估的最后一个字符。

或删除最后一个字符']',该字符不返回记录或不纠正表达式: [^ [:blank:]]

[^ \ s]是正确的。

,

也许更清晰的测试是生成一些包含各种空格字符的字符串,然后使用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:]",'[[:blank:]]') then 'Y' end as "[[:blank:]]",'[[:space:]]') then 'Y' end as "[[:space:]]",'\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:]匹配:blank中的任何一个,因为一个字符该类仅在[]方括号表达式内有效。

[[:blank:]]仅匹配空格。

[[:space:]]匹配制表符,换行符,回车符和空格。

\s[[:space:]]

以您的示例为例,它在行为上没有达到您期望的两种不同方式。

首先,[^[[:blank:]]]应该为[^[:blank:]]-即括号表达式中的字符类[:blank:]

第二,在没有空格的情况下,更正后的语法仍会返回匹配项,因为它查找的不是空格的任何字符,例如,第一个字符G不是空格,因此它与表达式匹配:

regexp_like('Greg94/Eric99Chandler/Faulkner','[^ ]');

要标识不包含任何空格字符的字符串,应使用:

not regexp_like(str,'\s')

not regexp_like(str,'[[:space:]]')
本文链接:https://www.f2er.com/2930579.html

大家都在问