Regex replace to a string without space at the end and beginning

Viewed 174

I want to remove the following characters {}, _ from a string and make it initcap.

{OTHER_QUESTIONS,MISSING_DOCUMENT}

Desired outcome:

Other Questions, Missing Document

I tried the following but it adds space to the first and last characters, so how can I achieve the desired outcome above?

regexp_replace(INITCAP('{OTHER_QUESTIONS,MISSING_DOCUMENT}'), '[{}_]', ' ', 'g'),
2 Answers

You can use

SELECT INITCAP(
  TRIM(
    REGEXP_REPLACE(REGEXP_REPLACE('{OTHER_QUESTIONS,MISSING_DOCUMENT}', ',(\S)', ', \1', 'g'), '[{}_]+', ' ', 'g')
  )
)

See an SQL fiddle.

The ,(\S) regex will match a comma and capture into Group 1 any non-whitespace char after it, and replace will , \1, that is, a comma, space and the Group 1 value. Then, [{}_]+ regex will match all occurrences of one or more {, } and _ chars and the regexp_replace will replace them with a space, and trim will remove the leading/trailing spaces.

just drop the space in Regex,' '-->'', replace:

regexp_replace(INITCAP('{OTHER_QUESTIONS,MISSING_DOCUMENT}'), '[{}_]', ' ', 'g')

to

regexp_replace(INITCAP('{OTHER_QUESTIONS,MISSING_DOCUMENT}'), '[{}_]', '', 'g')
Related