Reputation: 75
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?
Upvotes: 0
Views: 1903
Reputation: 8582
SELECT regexp_replace('1ABC 2ABC 3DEF 4DEF', '((\w*[^2]A|C)|(\w*[^4]D|F))','\1;','g');
Result: 1A;BC; 2ABC; 3D;EF; 4DEF;
Upvotes: 0