Frage

I am using posgresql, and I have encountered an issue with regular expressions and special characters.

select regexp_replace('asdf|asdf','|','.');

This function returns:

.asdf|asdf

Desired output:

asdf.asdf

How I can solve it? Please help :)

War es hilfreich?

Lösung

| is a special character in regex syntax called alternation, it means "or".

Your regex is selecting the empty string at the beginning of your string.

Try escaping it:

select regexp_replace('asdf|asdf','\|','.');

As @pozs underlined, for this particular task it is way more suited to use a simple replace:

select replace('asdf|asdf','|','.');
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top