Domanda

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

È stato utile?

Soluzione

| 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','|','.');
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top