Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8957353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:58:15+00:00 2026-06-15T14:58:15+00:00

To change tag pairs around text, this Postgres SELECT expression works for me: select

  • 0

To change tag pairs around text, this Postgres SELECT expression works for me:

select regexp_replace('The corpse of the huge <i>fin whale</i> created a spectacle on <span class="day">Friday</span> as <i>people</i> wandered the beach to observe it.',
                      '(<i>)([^/]+)(</i>)',
                      '<em>\2</em>',
                      'g');

I worry about excessive greed though on reference number two. My first try for reference number two was (.+) and that was a failure. The ([^/]+) works better. But I wonder if it is good enough.

Can anything be done to make that SELECT statement more robust?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:58:16+00:00Added an answer on June 15, 2026 at 2:58 pm

    There generally two possibilities (and both seem to be supported by PostreSQL’s regex engine).

    1. Make the repetition ungreedy:

      <i>(.+?)</i>
      
    2. Use a negative lookahead to ensure that you consume anything except for </i>:

      <i>((?:(?!</i>).)+)</i>
      

    In both cases, I removed the unnecessary captures. You can use \1 now in your replacement string.

    These two should be equivalent in what they do. Their performance might vary though. The former needs backtracking, while the latter has to attempt the lookahead at every single position. Which one is faster would have to be profiled and might even depend on individual input strings. Note that, since the second pattern uses a greedy repetition, you could remove the trailing </i> and you would still get the same results.

    The approach you have is already robust in the sense that you can never go past a </i>. But at the same time your approach does not allow nested tags (because the repetition could not go past the closing tag of the nested pair).

    However, you should note that regular expressions are not really up to the job of parsing/manipulating HTML. What if there are extraneous spaces in your tags? Or what if the opening tag has attributes? Or what if one or both of the tags occur in attribute names or comments?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this possible? example: $('a.change').click(function(){ //code to change p tag to h5 tag });
I need to change href tag with jquery, now i've got this. $(document).ready(function ()
How do I change the p tag text color css attribute using jQuery? I
I'm effectively trying to do the same as in this question Change #hash tag
I wanted to change a tag from being local to global by deleting and
I'd like to change the tag name of a MSXML XMLDOMElement, but unfortunately the
I was wondering whether breaking up a word with a span tag to change
I want to change class on click on a tag. The requirement is when
I'm using jQuery to change the HTML of a tag, and the new HTML
I need to know how to change the css class on a tag on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.