Forum: Ruby patten match

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D4edc2843c07e2b50be5c0850bd47fd8?d=identicon&s=25 geetha (Guest)
on 2007-06-19 07:25
(Received via mailing list)
Hi Good morning,

      I am having one text file. In this I need to remove all the new
lines
and I need to match the keyword what I am giving.

Please any one help me....

Thanks,
S.Sangeetha.
8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2007-06-19 08:11
(Received via mailing list)
On Jun 19, 12:24 am, geetha <sangeetha.geeth...@gmail.com> wrote:
> Hi Good morning,
>
>       I am having one text file. In this I need to remove all the new lines
> and I need to match the keyword what I am giving.
>
> Please any one help me....

Please don't be a "vampire":
http://slash7.com/articles/2006/12/22/vampires

Start with the official Ruby website, try to learn, then come back if
you have questions.

Jeff
softiesonrails.com
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2007-06-20 18:24
(Received via mailing list)
On 6/19/07, Jeff <cohen.jeff@gmail.com> wrote:
> On Jun 19, 12:24 am, geetha <sangeetha.geeth...@gmail.com> wrote:
> > Please any one help me....

cries someone

>
> Please don't be a "vampire": http://slash7.com/articles/2006/12/22/vampires

That whole article suggests -- albeit unintentionally -- a nice
comfortable ride towards elitism

Is "eitism" a word? I think I made it up.  I didn't have the patience
to look it up at www.m-w.com.  Just like I didn't have the patience to
read the entire ruby source code to answer my own questions so that I
won't waste anyone's time here :)
439c401f95ee2fac0be4c1727dd74dea?d=identicon&s=25 Bira (Guest)
on 2007-06-29 22:47
(Received via mailing list)
On 6/19/07, geetha <sangeetha.geethu05@gmail.com> wrote:
> Hi Good morning,
>
>       I am having one text file. In this I need to remove all the new lines
> and I need to match the keyword what I am giving.
>
> Please any one help me....
>
> Thanks,
> S.Sangeetha.

For removing newlines, you can use the String#gsub method, like this:

"one line\nother line".gsub("\n",'')

don't forget to use double quotes on the "\n" - they tell Ruby to
interpret the "slash-letter" combos as special characters, rather than
as a slash followed by a letter. "\n" is a newline.

For matching a word, you can use regular expressions - they're a
fairly interesting, but somewhat complicated topic, and you would be
much better served by looking for a Ruby regular expression tutorial
in Google.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-06-30 13:37
(Received via mailing list)
On 6/19/07, geetha <sangeetha.geethu05@gmail.com> wrote:
> Hi Good morning,
>
>       I am having one text file. In this I need to remove all the new lines
> and I need to match the keyword what I am giving.
>
> Please any one help me....
>
> Thanks,
> S.Sangeetha.

Hmm removing the newlines can be done in different ways, I'll show you
one below.
However it is not clear to me what you mean by matching keywords, as a
sample application I will replace the keyword by _keyword_, but maybe
you shall ask again.

def remove_nl_and_kw file_name, key_word="ruby"
   File.readlines(file_name).
     map{|line| line.chomp}.
     gsub(key_word, "_" << key_word << "_").
     join

end

If you had the newlines removed because keywords might spwan lines you
have to put join in front of gsub, but that might become a performance
nightmare for larger files as you will be calling gsub on *huge*
strings.

HTH
Robert
F62cfaca4a7a5bf58e08a664c2f41b94?d=identicon&s=25 Ian Whitlock (ian)
on 2007-06-30 15:45
Robert Dober wrote:

> def remove_nl_and_kw file_name, key_word="ruby"
>    File.readlines(file_name).
>      map{|line| line.chomp}.
>      gsub(key_word, "_" << key_word << "_").
>      join
>
> end
>
> If you had the newlines removed because keywords might spwan lines you
> have to put join in front of gsub, but that might become a performance
> nightmare for larger files as you will be calling gsub on *huge*
> strings.

Robert,

Nice tight illustrative code, but it needs a newbie correction.  If join
is to be at the end, then you need

def remove_nl_and_kw( file_name, key_word="ruby")
   File.readlines(file_name).
     map{|line| line.chomp.gsub(key_word, "_" << key_word << "_")}.
     join
end

Now I have another newbie question.  When is the file closed?
end of readlines?
end of statement?
end of function call?
end of program?

Thanks for patience.
Ian
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-06-30 16:38
(Received via mailing list)
On 6/30/07, Ian Whitlock <iw1junk@comcast.net> wrote:
> > If you had the newlines removed because keywords might spwan lines you
> > have to put join in front of gsub, but that might become a performance
> > nightmare for larger files as you will be calling gsub on *huge*
> > strings.
>
> Robert,
>
> Nice tight illustrative code, but it needs a newbie correction.  If join
> is to be at the end, then you need
Well spotted, thx for correcting it :), will teach me to copy lines
around.
>
> def remove_nl_and_kw( file_name, key_word="ruby")
>    File.readlines(file_name).
>      map{|line| line.chomp.gsub(key_word, "_" << key_word << "_")}.
that's it very nice
>      join
> end
>
> Now I have another newbie question.  When is the file closed?
> end of readlines?
that's it, as a matter of fact this is one of my favorite properties
of the class methods
#IO.read, #IO.readlines and #IO.open with a block.
of program?
>
> Thanks for patience.
> Ian
Thank *you*
Robert
This topic is locked and can not be replied to.