Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split words with a capital letter in PostgreSQL

I have sene Split words with a capital letter in sql which is applicable to MS SQL but I was wondering how to achieve the same using PostgreSQL

Basically I'm getting values such as FirstNameValue but I need it to be First Name Value

Unfortunately I don't know where to even start. I got to the following and got stuck straight away

SELECT REGEXP_REPLACE('ThoMasTest', '[^ ][A-Z].', ' ')

The result from a string such as ThoMasTest should be Tho Mas Test

Thanks

like image 973
pee2pee Avatar asked Nov 30 '25 07:11

pee2pee


1 Answers

This should do the trick:

select regexp_replace('ThoMasTest', '([a-z])([A-Z])', '\1 \2','g');

The expression matches two characters next to eachother, each one in its own group:

  1. [a-z] that matches a lowercase letter.
  2. [A-Z] finds a capital letter

So if one lowercase if immediately followed by a capital letter insert a space between them.

Do that globally 'g'.

like image 145
UlfR Avatar answered Dec 01 '25 22:12

UlfR