1

If I have this in a Postgresql 9.1 column:

foo foo <th id="ddd"> foo foo <th id="www"> foo

And I want it to update to this:

foo foo <th> foo foo <th> foo

I've tried regex_replace, but I have not succeeded.

2
  • See also stackoverflow.com/a/24417204 Commented Jun 25, 2014 at 20:36
  • 1
    Peter: Funny you add that two years later. Because I ended up going in a direction similar to one of the comments in the Q (I actually used the libxslt in Postgres). They warned me that one should parse with a parser, not with regex. At first I did not heed, then I learned my lesson. Commented Jun 28, 2014 at 19:26

1 Answer 1

1

Assuming you have a table like this:

CREATE TABLE table1
(
  a character varying NOT NULL,
  ...
)

You can use the following regexp_replace:

update table1 set a = regexp_replace(a, '(.*?)<(\w+)\s+.*?>(.*)', '\1<\2>\3', 'g');

The 'g' flag indicates to replace all matching patterns, not only the first one.

With this input:

foo foo <th id="ddd"> foo foo <th id="www"> foo<div id="hey">

I get the following ouput:

foo foo <th> foo foo <th> foo<div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.