From: Brian Candler on
Lucky Nl wrote:
> Hi
> I Need small help that how to remove leading   tags
> My text is:
> str = "<p>Welcome to ruby &nbsp;&nbsp;</p> <p>&nbsp;&nbsp;</p>"
> I want result is
> str = "<p>Welcome to ruby</p>"
>
> Can anybody help

You mean trailing, rather than leading?

You probably want String#gsub or String#gsub!. For example:

$ irb --simple-prompt
>> str = "<p>Welcome to ruby &nbsp;&nbsp;</p> <p>&nbsp;&nbsp;</p>"
=> "<p>Welcome to ruby &nbsp;&nbsp;</p> <p>&nbsp;&nbsp;</p>"
>> str.gsub!(/(&nbsp;|\s)+/, " ")
=> "<p>Welcome to ruby </p> <p> </p>"
>>

Removing empty paragraphs is left as an exercise. For more information
on String and Regexp see http://www.ruby-doc.org/docs/ProgrammingRuby/

However for anything other than the most basic transformations, you are
almost certainly better off with a HTML parser like Nokogiri, than
chomping HTML with regexps.
--
Posted via http://www.ruby-forum.com/.

From: Lucky Nl on
Hi ,
Am entering multiple paragrpahs in editior .that will be saved into str
varable.

Ex:
str is
<p>test one &nbsp;&nbsp;</p><p>&nbsp;&nbsp;</p><p>test one test
onetest onetest one</p> <p>test two test
two test two test two &nbsp;&nbsp;</p> <p>&nbsp;&nbsp;</p>

we have enetered like this way
I want result is

<p>test one &nbsp;&nbsp;</p><p>&nbsp;&nbsp;</p><p>test one test
onetest onetest one</p> <p>test two test
two test two test two</p>


here removed end of the nbsptags between paragparhs and removed nbsp; 's
in "<p>test two test
two test two test two</p>"
--
Posted via http://www.ruby-forum.com/.

From: Brian Candler on
Lucky Nl wrote:
> Hi ,
> Am entering multiple paragrpahs in editior .that will be saved into str
> varable.
>
> Ex:
> str is
> <p>test one &nbsp;&nbsp;</p><p>&nbsp;&nbsp;</p><p>test one test
> onetest onetest one</p> <p>test two test
> two test two test two &nbsp;&nbsp;</p> <p>&nbsp;&nbsp;</p>
>
> we have enetered like this way
> I want result is
>
> <p>test one &nbsp;&nbsp;</p><p>&nbsp;&nbsp;</p><p>test one test
> onetest onetest one</p> <p>test two test
> two test two test two</p>
>
>
> here removed end of the nbsptags between paragparhs and removed nbsp; 's
> in "<p>test two test
> two test two test two</p>"

Your requirement is unclear. Are you saying you want to remove the
&nbsp;'s within the fourth paragraph only, and remove the fifth
paragraph entirely?

I've shown you how to use gsub, and where to find more documentation on
it. String#scan might be useful too.

I suggest you use them in whatever way you need, since only you
understand what you're trying to achieve.
--
Posted via http://www.ruby-forum.com/.

From: MrZombie on
On 2010-08-02 07:22:33 -0400, Lucky Nl said:

> <p>test one &nbsp;&nbsp;</p><p>&nbsp;&nbsp;</p><p>test one test
> onetest onetest one</p> <p>test two test
> two test two test two &nbsp;&nbsp;</p> <p>&nbsp;&nbsp;</p>

str = str.gsub(/&nbsp;/,"").gsub(/<p>\s*<\/p>/,"")

This will remove any &nbsp; from your html, and after that, remove any
<p> tag that contained only whitespace character.

It's less than optimal, as you could combine it in one go, probably,
but I don't want to spend time on stuff you should be able to do on
your own.
--
Thank you for your brain.
-MrZombie

From: Lucky Nl on
Hi ,
Let me explain my requiremnt clearly.
Am usinng fck editor in rubyonrails.
So I can enter data is multiple paragraphs or single paragraph. but
after the last paragraph if there is any spaces , i want to remove them