pee2pee
pee2pee

Reputation: 3802

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

Upvotes: 2

Views: 2495

Answers (1)

UlfR
UlfR

Reputation: 4395

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'.

Upvotes: 9

Related Questions