1

The regexp [[:blank:]] and \s arent they the same. The below shows 2 different results.

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

The above query returns no rows whereas when i replace blank with [^/s] it returns the row.

alexherm
  • 1,362
  • 2
  • 18
  • 31
arsha
  • 67
  • 6
  • What is the pattern you want to match? For example, `[^x]` matches any character that is not `x`. Do you really want to test whether `'Greg94/Eric99Chandler/Faulkner'` contains any character that is not a whitespace character, or do you actually want to check whether it contains any whitespace? – William Robertson Dec 14 '19 at 16:50
  • i am looking to return the string that has no blanks in it . In the example, the string has no blanks so the query should return that row. i used the correct syntax and now i get it.Thank you – arsha Dec 17 '19 at 19:30

3 Answers3

1

That would be

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','/','') as result
  2  FROM dual
  3  WHERE REGEXP_LIKE(TRIM('Greg94/Eric99Chandler/Faulkner'), '[^\s]');

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

SQL>

Pick the one you like the most. Besides, if you found what works OK, why don't you simply use it (and forget about the one that doesn't work)? (I guess I know - because of but WHY???).

Littlefoot
  • 131,892
  • 15
  • 35
  • 57
1

the problem is that you are using [[:blank:]] instead of [:blank:]. Regular Expression [^ [[:blank:]]] evaluate:

  1. ^[[:blank:]] : no character within the list "[, [:blank:]"
  2. ] last character to be evaluated.

or you remove the last character ']' which is the one that is not returning records or correct the expression: [^ [:blank:]]

[^\s] is correct.

  • In a regexp \s and the class [:blank:] are NOT the same; \s actually corresponds to the POSIX class [:space:] which is much more inclusive. [:blank:] is space and tab; [:space:] is space, tab, form feed, etc. See [Oracle POSIX](https://docs.oracle.com/cd/E11882_01/appdev.112/e41502/adfns_regexp.htm#ADFNS233) and a [POSIX description](https://docs.oracle.com/cd/E11882_01/appdev.112/e41502/adfns_regexp.htm#ADFNS233). – Belayer Dec 13 '19 at 17:21
  • Yes, I do not say otherwise, [: blank:] and \s are different but not because the queries have different results. The queries produce different results because one of them uses a regular expression incorrect for the case. – Juan Carlos Guibovich Dec 13 '19 at 19:12
  • You have a space in your character class - `[: blank:]` should be `[:blank:]` – William Robertson Dec 18 '19 at 11:49
0

Perhaps a clearer test would be to generate some strings containing various whitespace characters and then use case expressions to see whether they match different regexes.

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

(I manually edited the result for the row with tabs to align the results, otherwise the tab messes it up and makes it harder to read.)

[:blank:] matches any of :, b, l, a, n, k, because a character class is only valid within a [] bracket expression.

[[:blank:]] only matches spaces.

[[:space:]] matches tab, newline, carriage return and space.

\s is the same as [[:space:]]

As for your example, it is not behaving as you expected in two different ways.

Firstly, [^[[:blank:]]] should be [^[:blank:]] - that is, the character class [:blank:] within a bracketed expression.

Secondly, the corrected syntax still returns a match when there are no blanks because it looks for any character that is not a space, for example the first character G is not a space so it matches the expression:

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

To identify strings that do not contain any whitespace character, you should use:

not regexp_like(str,'\s')

or

not regexp_like(str, '[[:space:]]')
William Robertson
  • 15,273
  • 4
  • 38
  • 44