E-Madd
E-Madd

Reputation: 4582

How to replace a variable number of instances in a string?

A front end WYSIWYG editor in our application has been producing strings with gobs of <p> tags in the editable string. Every time the records were updated, the <p> tags would double. How can I fix the data, reducing n number of consecutive <p> tags in the string with a single one?

Change this:

<p><p>Hello, world

or this:

<p><p><p><p><p><p><p><p><p>Hello, world

to this:

<p>Hello, world

Upvotes: 0

Views: 26

Answers (1)

Ramin Faracov
Ramin Faracov

Reputation: 3303

You can do it with using regular expression. Example:

select regexp_replace('<p><p><p><p><p><p><p><p><p>Hello, world', '(<p>)\1{1,}', '\1', 'g');

Result: 
<p>Hello, world

Upvotes: 2

Related Questions