0

i want to replace all the 'Spaces' with ' & ' excluding ' | ' in regexp_replace

For Ex: 'create | delete account' -> expect Output as 'create | delete & account'.

I m trying with the sql

select regexp_replace('create | delete account','\s [^\s\|\s]',' & ') from dual

But i m doing something wrong here. Could anyone please help on it.

1
  • If you have a sequence of spaces, do you need to replace every space with ' & '? Also, is it possible that you have the character '&' in your starting string? Commented Dec 29, 2016 at 13:48

2 Answers 2

3

I think this is what you want:

select regexp_replace('create | delete account', '([^\|]) ([^\|])', '\1 & \2')
from dual

The logic here is to make the replacement whenever a space should occur between two non pipe characters. I capture these characters using parentheses, and then use them in the replacement via \1 and \2.

Sign up to request clarification or add additional context in comments.

4 Comments

Use ([^\|])\s+([^\|]) for the case of more than 3 spaces. +1 though.
@GurwinderSingh Thanks for the comment and upvote :-)
@ Tim Biegeleisen - Thank you .. It solved .. I liked the way you seen the requirement (ie) 'Replace space between two non pipe character'
This may work for the OP, but how about text like this: 'a | cos(b) exp(c) | z |\lambda|'? The pipe symbol may appear with space on one side only, and that space SHOULD be replaced with ' & ' according to the requirement.
0

If you need to replace every single space with the string ' & ', and there's no '&' in your starting string, you may avoid regular expressions:

select replace ( replace('create | delete account', ' ', ' & '), ' & | & ', ' | ') from dual

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.