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 > 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('^>.*','<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.
You can use
s = "> This is a blockquote line\ntesting a new line\n> 1- Another new blockquote section\n> 2- And this is part of the same blockquote\n> And this is the final line of this blockquote\ntesting another\n> 1- Another new blockquote\n> 2- And the final line of the 3rd blockquote"
rx = /^>(.*)/
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 ^>(.*) matches start of a line with ^, then matches > 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.
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[/^>/]
block << line.sub(/^>/,"")
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