Re: Regular expression to UPPER() a lower case string

From: "Peter J(dot) Holzer" <hjp-pgsql(at)hjp(dot)at>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Regular expression to UPPER() a lower case string
Date: 2022-12-10 16:16:06
Message-ID: 20221210161606.dzd4lmxahtijqrm5@hjp.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 2022-12-10 15:37:01 +0000, Eagna wrote:
> Hi, and thanks for all of the input - I think I'm beginning to grok it.

I'm not so sure about that

> As far as I can see, I can only do what I want by using the following.
>
> If there's a monthly prize on this list for the most insanely
> contrived piece of code, I think this is a strong contender:
>
> test.x = 'abc'
>
> SELECT
> UPPER(REGEXP_REPLACE(x, '(.)(.)(.)', '\1')) ||
> UPPER(REGEXP_REPLACE(x, '(.)(.)(.)', '\2')) ||
> UPPER(REGEXP_REPLACE(x, '(.)(.)(.)', '\3'))
> FROM test;
>
> Result: ABC

I don't think this does what you want it to do:

wds=> SELECT x,
UPPER(REGEXP_REPLACE(x, '(.)(.)(.)', '\1')) ||
UPPER(REGEXP_REPLACE(x, '(.)(.)(.)', '\2')) ||
UPPER(REGEXP_REPLACE(x, '(.)(.)(.)', '\3'))
FROM test;
╔═════════════╤═════════════════════════════╗
║ x │ ?column? ║
╟─────────────┼─────────────────────────────╢
║ abc_def_ghi │ A_DEF_GHIB_DEF_GHIC_DEF_GHI ║
╚═════════════╧═════════════════════════════╝

(I admit it took me at least a minute to figure out what was happening
here)

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp(at)hjp(dot)at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2022-12-10 16:46:50 Re: Regular expression for lower case to upper case.
Previous Message Eagna 2022-12-10 15:37:01 Re: Regular expression to UPPER() a lower case string