Someone helped me earlier today with a method to remove the “pre” tag
block and replace it with ‘’. This is because when I’m showing snippets
of a post, I don’t want a piece of code to be in the snippet. Anyway,
here’s what I’m doing:
works perfect (only for pre)
def strip_pre_block
text.gsub(/
[^<]*</pre>/,’’)
end
Now, I want to do the same with blockquote. How can I combine the two?
Something like this:
doesn’t work at all (for either one)
def strip_pre_and_quote_blocks
text.gsub(/(^
[^<]</pre>$|^
.
</blockquote>$)/,’’)
end
When I try that, it doesn’t replace either one of the “pre” or
“blockquote” blocks with ‘’, but the first method works perfect for the
“pre” block only. I can’t even get the blockquote one to work by
itself.
Can someone help out with this??? Thanks in advance…
Here are some general suggestions. Write a test case for this regexp,
have
it print out a result, and run it over and over again as you
incrementally
add elements. I note this because you appear to have leapt directly from
a
simple to a complex regexp without incrementally examining the reaction
to
each individual symbol. Get used to writing test cases as experiments,
and
frequently running them (with one keystroke preferrably).
Next, put the ^ and $ outside the (), on principle.
Next, try (
|
).
Next, [^<]* is always tempting, but you can get less greedy IIRC with
.*?.
That will look-ahead a little more.
Next, post this to a Ruby or Regex newsgroup, because it’s not about
Rails.
Next, treat your HTML as XHTML and parse it with REXML. You can use
XPath to
reach in and nab each pre, and change its tag or contents to whatever
you
want. Then write it all back as XHTML.
Next, each < probably needs an escape, like <
When I try that, it doesn’t replace either one of the “pre” or
“blockquote” blocks with ‘’, but the first method works perfect for the
“pre” block only. I can’t even get the blockquote one to work by
itself.
You could run two gsubs; one with
and one with
.
To strip, you could just yank all 4 items with 4 gsubs, too.
irb>> html_fragment = <<EOF
And then someone said:
When I try that, it doesn't replace either one of the "pre" or
"blockquote" blocks with '', but the first method works perfect for the
"pre" block only. I can't even get the blockquote one to work by
itself.
So I told them to use this code:
regexp = %r{<(pre|blockquote)\\b[^>]*>.*?}m
my_string = <<-EOS
And then someone said:
When I try that, it doesn't replace either one of the "pre" or
"blockquote" blocks with '', but the first method works perfect for the
"pre" block only. I can't even get the blockquote one to work by
itself.
EOS
my_string.gsub!(regexp, '...')
When I try that, it doesn't replace either one of the "pre" or
"blockquote" blocks with '', but the first method works perfect for the
"pre" block only. I can't even get the blockquote one to work by
itself.
So I told them to use this code:
regexp = %r{<(pre|blockquote)\\b[^>]*>.*?}m
my_string = <<-EOS
And then someone said:
When I try that, it doesn't replace either one of the "pre" or
"blockquote" blocks with '', but the first method works perfect for the
"pre" block only. I can't even get the blockquote one to work by
itself.
EOS
my_string.gsub!(regexp, '...')
EOF
=> "And then someone said:\n
\nWhen I try that, it doesn't
replace either one of the \"pre\" or\n\"blockquote\" blocks with '',
but the first method works perfect for the\n\"pre\" block only. I
can't even get the blockquote one to work by\nitself.\n
\nWhen I try that, it doesn't replace
either one of the \"pre\" or\n\"blockquote\" blocks with '', but the
first method works perfect for the\n\"pre\" block only. I can't even
get the blockquote one to work by\nitself.\n
\n EOS\n
my_string.gsub!(regexp, '...')\n
\n"
irb>> html_fragment.gsub(regexp, ‘…’)
=> “And then someone said:\n…\nSo I told them to use this code:\n…
\n”
irb>> puts _
And then someone said:
…
So I told them to use this code:
…
=> nil
irb>>
In addition to using an XML parser as Philip suggests, you might want
to actually read about regular expressions. You can start with the
pickaxe (Programming Ruby, The Pragmatic Programmers’ Guide, 2nd ed.)
pages 68-70, 324-328, 600-603.