SO i entered data is like.
---------------------------------------------------------------
<p> pargraph1 pargraph1 </p>
<p>pargraph2 pargraph2 pargraph2 </p>
<p> pargraph3 hello3 hell13 </p>
<p> pargraph4 hello3 hell14 </p>
<p> </p>
--------------------------------------------------------
In the above text 4th paragraph is the last paragrph which i
entered.after that i was pressed enter button so editor converted this
into " <p> </p"
I want to remove nbsp's after text in last paragrpah means result look
like
-------------------------------------------
<p> pargraph1 pargraph1 </p>
<p>pargraph2 pargraph2 pargraph2 </p>
<p> pargraph3 hello3 hell13 </p>
<p> pargraph4 hello3 hell14</p>
----------------------------------------------------------------
So the approach is:
(1) Write a regular expression which matches just the thing you want to
delete;
(2) Invoke it with gsub to replace that text with the empty string.
For example, to delete *all* empty paragraphs, then you want to match
<p> followed by any mixture of and space followed by </p>. So you
could write:
str.gsub! /<p>( |\s)*<\/p>/, ''
(x|y) means match x or y, \s means match any whitespace character, and *
means match it 0 or more times.
To delete only the *last* paragraph if it is empty, then you can tweak
it to:
str.gsub! /<p>( |\s)*<\/p>\s*\z/, ''
where \z matches the end of the string, and \s* allows 0 or more space
characters, including newlines, to precede that.
Once you're happy with that, then you can do another match and replace
to change the final instance of " </p>" into just "</p>"
But you might want to be sure this is what you really want. How did the
previous entries get there? Do you really want to keep them? It
would be much simpler just to replace all sequences of or space
with a single space.
str.gsub! /( |\s)+/, ' '