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 3723496
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:17:30+00:00 2026-05-19T06:17:30+00:00

I have a text blob field in a MySQL column that contains HTML. I

  • 0

I have a text blob field in a MySQL column that contains HTML. I have to change some of the markup, so I figured I’ll do it in a ruby script. Ruby is irrelevant here, but it would be nice to see an answer with it. The markup looks like the following:

<h5>foo</h5>
  <table>
    <tbody>
    </tbody>
  </table>

<h5>bar</h5>
  <table>
    <tbody>
    </tbody>
  </table>

<h5>meow</h5>
  <table>
    <tbody>
    </tbody>
  </table>

I need to change just the first <h5>foo</h5> block of each text to <h2>something_else</h2> while leaving the rest of the string alone.

Can’t seem to get the proper PCRE regex, using Ruby.

  • 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-05-19T06:17:32+00:00Added an answer on May 19, 2026 at 6:17 am
    # The regex literal syntax using %r{...} allows / in your regex without escaping
    new_str = my_str.sub( %r{<h5>[^<]+</h5>}, '<h2>something_else</h2>' )
    

    Using String#sub instead of String#gsub causes only the first replacement to occur. If you need to dynamically choose what ‘foo’ is, you can use string interpolation in regex literals:

    new_str = my_str.sub( %r{<h5>#{searchstr}</h5>}, "<h2>#{replacestr}</h2>" )
    

    Then again, if you know what ‘foo’ is, you don’t need a regex:

    new_str = my_str.sub( "<h5>searchstr</h5>", "<h2>#{replacestr}</h2>" )
    

    or even:

    my_str[ "<h5>searchstr</h5>" ] = "<h2>#{replacestr}</h2>"
    

    If you need to run code to figure out the replacement, you can use the block form of sub:

    new_str = my_str.sub %r{<h5>([^<]+)</h5>} do |full_match|
      # The expression returned from this block will be used as the replacement string
      # $1 will be the matched content between the h5 tags.
      "<h2>#{replacestr}</h2>"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Server CE 4.0 table that has a text blob field
I have text file with some stuff that i would like to put into
I have text stored in SQL as HTML. I'm not guaranteed that this data
I have list of country names or other strings in a text blob field.
I have problem when I try insert some data to Informix TEXT column via
I have a blob of text that I need to split up with a
I have text within a paragraph tag that that fits snug on the bottom
I have text I am displaying in SIlverlight that is coming from a CMS
I have a text blob and I would like to select URL's based on
I have a table with a primary key that is a varchar(255). Some cases

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.