Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

299
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda