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.
geetha (Guest)
on 2007-06-19 09: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.
Jeff C. (Guest)
on 2007-06-19 10:11
(Received via mailing list)
On Jun 19, 12:24 am, geetha <removed_email_address@domain.invalid> 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
Todd B. (Guest)
on 2007-06-20 20:24
(Received via mailing list)
On 6/19/07, Jeff <removed_email_address@domain.invalid> wrote:
> On Jun 19, 12:24 am, geetha <removed_email_address@domain.invalid> 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 :)
Bira (Guest)
on 2007-06-30 00:47
(Received via mailing list)
On 6/19/07, geetha <removed_email_address@domain.invalid> 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.
Robert D. (Guest)
on 2007-06-30 15:37
(Received via mailing list)
On 6/19/07, geetha <removed_email_address@domain.invalid> 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
Ian W. (Guest)
on 2007-06-30 17:45
Robert D. 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
Robert D. (Guest)
on 2007-06-30 18:38
(Received via mailing list)
On 6/30/07, Ian W. <removed_email_address@domain.invalid> 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.