What is the main difference between '[[:space:]]' and '\s' in Oracle pl-sql regular expression?

dba.stackexchange https://dba.stackexchange.com/questions/270531

문제

I have a string like this x=y AND z=y AND p=q , I need to replace every (space)AND(space) pattern with * so that the original string turns into x=y*z=y*p=q. I'm using Regular expression for this cause the important thing is that there might by more that one space before and after each AND .

I've written two different queries for this

 query1:

 select regexp_replace('x=y AND z=y AND p=q','[[:space:]]AND[[:space:]]','*')
 from dual;


query2:

 select regexp_replace('x=y AND z=y AND p=q', '\s+AND\s+', '*')
 from dual;

I want to know :

  1. Are there better ways to do that?
  2. What is the main difference between these two queries? I mean[[:space:]] and \s? The important thing is that I want to know In what sort of condition these two queries will produce different result?

Thanks in advance

도움이 되었습니까?

해결책

The Oracle documentation confirms that the POSIX expression [[:space:]] is equivalent to the Perl-style \s. I personally prefer \s because it's shorter, and also supported by many text editors.

In your example you have [[:space:]] (one whitespace character) but \s+ (one or more whitespace characters). Maybe that should have been [[:space:]]+.

다른 팁

Those anchors are used whenever you want to have something very general as it says in the regular expression processors they care about "Any space character" but then the definition in POSIX is more elaborated and says "space, tab, newline". It depends on your expression of how it is written because technically using these two alternatives should not introduce any bug or difference in behavior.

\\s → any space character
\\S → anything but a space character
--
[[:space:]] → space, tab, newline etc.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 dba.stackexchange
scroll top