I have a string like this x=y and g=h and u=1
and I need to replace each (space)and(space)
with ,
. I'm using the powerful regular_expression
for this but it does not give me the desired result.
select regexp_replace('x=y and g=h and u=1','(^[[:space:]]*)AND(^[[:space:]]*)', ',')
from dual;
I was wondering if you could help me out here. thanks in advance.
Best Answer
If the format is always exactly one space either side of a lowercase
and
, then you don't needregexp_replace
as plain oldreplace
will do it:A regex is needed if the number of spaces can vary:
The
+
means one or more, in contrast to*
which means zero or more. For example I assume you don't wantBand in Spandex
replaced withB,in Sp,ex
.To be on the safe side, I've used the Perl-style
\s
(whitespace) operator rather than a literal space character, as this will also match any tab characters.