0

I need help getting my regex syntax right in Postgres regexp_replace: My string:

1ABC 2ABC 3DEF 4DEF  

My 2 Match/Replace conditions are:

 Replace: A OR C but not in front of 2
    or
 Replace: D OR F but not in front of 4

So I'm expecting to get:

"1A;BC; 2ABC; 3D;EF; 4DEF;"

My partial replace for condition 1 is:

SELECT regexp_replace('1ABC 2ABC 3DEF 4DEF', '((?<!2)(A|C))','\1;','g' );

My 'Replace' is really an 'Insert' after the matched literal.

I just can not seem to be able to find the pattern for the 2nd condition, without breaking the whole thing. Is this even possible in 1 statement?

1 Answer 1

0

SELECT regexp_replace('1ABC 2ABC 3DEF 4DEF', '((\w*[^2]A|C)|(\w*[^4]D|F))','\1;','g');

Result: 1A;BC; 2ABC; 3D;EF; 4DEF;

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

1 Comment

And eurotrash is a genius! :-) Thanks!

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.