Question

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 :)

Was it helpful?

Solution

| 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','|','.');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top