Question

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

Was it helpful?

Solution

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:]]+.

OTHER TIPS

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top