0

I want to replace E-*-[_]F* string into E-*-\_F*. The code I am using is below.

select regexp_replace('E-*-[_]F*','-[\[(.)\]]', E'\\', 'g'); -- E-*\_]F*

I am not able to remove the closing bracket.

1
  • 2
    Your input and output are not clear. Please show us some sample data. Commented Jan 31, 2018 at 7:35

1 Answer 1

1

assuming you want the character inside the braces to be placed after a backslash:

jasen=#  select regexp_replace('E-*-[_]F*','-\[(.)\]', '\\\1', 'g');
 regexp_replace 
----------------
 E-*\_F*
(1 row)

The pattern looks for any character (.) between -[ and ] the parentheses make it remember the character.

The whole matched part is replaced with a backslash, represented by \\ , followed by the first (and only) remembered part \1.

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

1 Comment

Thanks, @Jasen . This works. can you explain this a little?

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.