2

How can I use regex in Postgres to replace a capture with an upper case version of itself.

regexp_replace(pf.description, '^(.)(.*)$', '\U\1\E\2', 'gi') as description

is giving me the string back with the literal values \U and \E.

6
  • possible duplicate of Replacing regex matched text with uppercase version in Postgresql Commented Jun 30, 2014 at 19:40
  • Are you using an outdated version of Postgres or why do you escape literal characters? Commented Jun 30, 2014 at 19:46
  • @ErwinBrandstetter What literal characters have I escaped? Commented Jun 30, 2014 at 19:49
  • \U and \E, or do you need that literally? Commented Jun 30, 2014 at 19:50
  • @ErwinBrandstetter \U and \L are how I change case using regex. I am just trying to make the first letter of the string upper case. Commented Jun 30, 2014 at 19:55

1 Answer 1

4

There is no built-in regex functionality in Postgres to convert to upper / lower case (that I'd know of).

I would use left() and right() instead:

SELECT upper(left('test_string', 1))
    || lower(right('test_string', -1));

Result:

Test_string

Postgres regular expression functionality in the manual.

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

1 Comment

Works nicely. Thanks much.

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.