From: Lucky Nl Date: 2010-08-03T18:57:11+09:00 Subject: Re: How to remove leading   from string Brian Candler wrote: >> SO i entered data is like. >> --------------------------------------------------------------- >>

pargraph1 pargraph1   

>>

pargraph2 pargraph2 pargraph2   

>>

pargraph3 hello3 hell13   

>>

pargraph4 hello3 hell14   

>>

    

>> -------------------------------------------------------- >> >> 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 "

    > >> I want to remove nbsp's after text in last paragrpah means result look >> like >> ------------------------------------------- >>

pargraph1 pargraph1   

>>

pargraph2 pargraph2 pargraph2   

>>

pargraph3 hello3 hell13   

>>

pargraph4 hello3 hell14

>> ---------------------------------------------------------------- > > 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 >

followed by any mixture of   and space followed by

. So you > could write: > > str.gsub! /

( |\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! /

( |\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 "  

" into just "

" > > 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)+/, ' ' Hi when i was used below logic. str.gsub! /

( |\s)*<\/p>\s*\z/, '' ------------------------------------------------- str = "

pargraph1 pargraph1   

pargraph2 pargraph2 pargraph2   

pargraph3 hello3 hell13   

pargraph4 hello3 hell14   

  

" str = str.gsub! /

( |\s)*<\/p>\s*\z/, '' puts str --------------------------------- its returns below resule and it is fine. ----------------------------------------------

pargraph1 pargraph1   

pargraph2 pargraph2 pargraph2   

pargraph3 hello3 hell13   

pargraph4 hello3 hell14   

--------------------------------------------- But if i gave with littile modifiaction at endof line is enetered with chars

dada  ddsa

" str giving result nil -- Posted via http://www.ruby-forum.com/.