Re: Regular expression for lower case to upper case.

Поиск
Список
Период
Сортировка
От Peter J. Holzer
Тема Re: Regular expression for lower case to upper case.
Дата
Msg-id 20221210133150.4l57ps6lk5dcddd4@hjp.at
обсуждение исходный текст
Ответ на Re: Regular expression for lower case to upper case.  (Eagna <eagna@protonmail.com>)
Ответы Re: Regular expression for lower case to upper case.  ("David G. Johnston" <david.g.johnston@gmail.com>)
Список pgsql-general
On 2022-12-10 11:00:48 +0000, Eagna wrote:
> > RegExp by itself cannot do this. You have to match all parts of the
> > input into different capturing groups, then use lower() combined
> > with format() to build a new string. Putting the capturing groups
> > into an array is the most useful option.
>
> OK - I *_kind_* of see what you're saying.
>
> There's a small fiddle here (https://dbfiddle.uk/rhw1AdBY) if you'd
> care to give an outline of the solution that you propose.

For example like this:

INSERT INTO test VALUES
('abc_def_ghi');

Let's say I want to uppercase the part between the two underscores.

First use regexp_replace to split the string into three parts: One
before the match, the match and one after the match:

SELECT
  regexp_replace(x, '(.*_)(.*)(_.*)', '\1'),
  regexp_replace(x, '(.*_)(.*)(_.*)', '\2'),
  regexp_replace(x, '(.*_)(.*)(_.*)', '\3')
FROM test;

╔════════════════╤════════════════╤════════════════╗
║ regexp_replace │ regexp_replace │ regexp_replace ║
╟────────────────┼────────────────┼────────────────╢
║ abc_           │ def            │ _ghi           ║
╚════════════════╧════════════════╧════════════════╝
(1 row)


Once that works, uppercase the part you want and concatenate everything
together again:

SELECT
  regexp_replace(x, '(.*_)(.*)(_.*)', '\1') ||
  upper(regexp_replace(x, '(.*_)(.*)(_.*)', '\2')) ||
  regexp_replace(x, '(.*_)(.*)(_.*)', '\3')
FROM test;

╔═════════════╗
║  ?column?   ║
╟─────────────╢
║ abc_DEF_ghi ║
╚═════════════╝
(1 row)

        hp

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

Вложения

В списке pgsql-general по дате отправления:

Предыдущее
От: "Peter J. Holzer"
Дата:
Сообщение: Re: Regular expression to UPPER() a lower case string
Следующее
От: Eagna
Дата:
Сообщение: Re: Regular expression to UPPER() a lower case string