Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

295
Views
Ruby - Add tag either side of matched regex with gsub

I have a regex here: https://rubular.com/r/3p9WVitnlZQU3i

When pinging the slack API, it converts blockquotes into individual lines with > at the start of each line even if there are multiple lines. I want to wrap them in blockquotes:

Just in case the link expires at some point in the future, here is the test string to be matched:

> This is a blockquote line
testing a new line
> 1- Another new blockquote section
> 2- And this is part of the same blockquote
> And this is the final line of this blockquote
testing another
> 1- Another new blockquote
> 2- And the final line of the 3rd blockquote

And here is the regex

^>.*

The regex works fine and I am trying to use gsub or another method to wrap them in blockquotes so the final result would be:

<blockquote>This is a blockquote line</blockquote>
testing a new line
<blockquote>1- Another new blockquote section
2- And this is part of the same blockquote
And this is the final line of this blockquote</blockquote>
testing another
<blockquote>1- Another new blockquote
2- And the final line of the 3rd blockquote</blockquote>

Or even if that is very difficult, if it just wrapped every line that started with &gt; in blockquote tags that would be fine either as I can just add no bottom margin and they will look like one large quote.

I honestly am a bit lost but I guess I am trying to do something like this:

wrap_in_blockquote = replace_backticks2.gsub('^&gt;.*','<blockquote>/0</blockquote>')

But I have no idea if I need to iterate through the matches individually or this will do that or what syntax I should use as I am not familiar with gsub and struggled with the documentation. Any help appreciated.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use

s = "&gt; This is a blockquote line\ntesting a new line\n&gt; 1- Another new blockquote section\n&gt; 2- And this is part of the same blockquote\n&gt; And this is the final line of this blockquote\ntesting another\n&gt; 1- Another new blockquote\n&gt; 2- And the final line of the 3rd blockquote"
rx = /^&gt;(.*)/
puts s.gsub(rx, '<blockquote>\1</blockquote>')

See the Ruby online demo. Output:

<blockquote> This is a blockquote line</blockquote>
testing a new line
<blockquote> 1- Another new blockquote section</blockquote>
<blockquote> 2- And this is part of the same blockquote</blockquote>
<blockquote> And this is the final line of this blockquote</blockquote>
testing another
<blockquote> 1- Another new blockquote</blockquote>
<blockquote> 2- And the final line of the 3rd blockquote</blockquote>

Details:

The ^&gt;(.*) matches start of a line with ^, then matches &gt; and then captures into Group 1 any zero or more chars other than line break chars as many as possible with (.*), where the parentheses create a capturing group with ID 1 (hence, \1 is used in the replacement pattern to put back the group value into the resulting string).

Note the single quotes in the replacement pattern string literal, if you use double quotes, you would need to double the backslashes.

over 4 years ago · Santiago Trujillo Report

0

Sometimes it's easier to just use code in stead of a very complex regular expression. It will be more readable and afterward if you need to change something you or your colleagues will be thankful.

text = DATA.read
block = "<blockquote>"
text.each_line do |line|
  if line[/^&gt;/]
      block << line.sub(/^&gt;/,"")
  else
      unless block=="<blockquote>"
        puts "#{block.chomp}</blockquote>" 
        block = "<blockquote>"
      end
      puts line
    end
end
puts block.chomp << "</blockquote>" unless block=="<blockquote>"

Which gives what you asked

<blockquote> This is a blockquote line</blockquote>
testing a new line
<blockquote> 1- Another new blockquote section
 2- And this is part of the same blockquote
 And this is the final line of this blockquote</blockquote>
testing another
<blockquote> 1- Another new blockquote
 2- And the final line of the 3rd blockquote</blockquote>

See here for a working demo

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!