regex - psql uppercase backreferenced string in regexp_replace -
i have string, processed initcap(), , wanto uppercase part of it.
to specific - want uppercase basic roman digits might occur.
to more specific i'd replace
jana iii sobieskiego
to
jana iii sobieskiego
i suppose use kind of upper-substring-subquery combo achieve it, trying make work in single regexp_replace, this:
select ulica --, regexp_matches(ulica , '((^|\s)([xxiivv]+)(\s|$))', 'g') , regexp_replace(ulica, '((^|\s)([xxiivv]+)(\s|$))', '\2'||upper('q\3q')||'\4' , 'g') ( select unnest(array['jana iii sobieskiego', 'xx lecia', 'xxx lecia panowania zygmunta iii wazy'])::text ulica ) src
what happens, upper works on 'static' part of replacement string (q...q), not on backreference.
i
jana qiiiq sobieskiego
anyone has idea how this?
postgresql 9.1
short answer
unfortunately, have tried not possible regexp_replace
.
long answer
intro
line
regexp_replace(ulica, '((^|\s)([xxiivv]+)(\s|$))', '\2'||upper('q\3q')||'\4' , 'g')
is equivalent to
regexp_replace(ulica, '((^|\s)([xxiivv]+)(\s|$))', '\2q\3q\4' , 'g')
as can see, regexp_replace
won't upper case backreferences.
workaround
can create own function take ulica
parameter , returns ulica
basic roman digits uppercased.
- step 1
in first step function mark (i choose$$
marker can use any.) part of ulica uppercased this:
regexp_replace(ulica, '((^|\s)([xxiivv]+)(\s|$))', '\2$$\3$$\4' , 'g')
- step 2
in second step, go through resulting string , uppercase each char located between 2 markers.
Comments
Post a Comment