Forum: Ruby help with regular expressions

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.
0c773c65707ec2ff412d05b7559af343?d=identicon&s=25 Vandana (Guest)
on 2008-10-24 23:06
(Received via mailing list)
Hi all,

I'm fairly new to programming with regular expressions.  I would like
some help with the
following. Consider the format given below as what I will receive in a
file.

While reading from the file, Im interested only in reading those lines
that have the forward-slash' in
them (lines 9,10,12, 13). How can I do this in ruby?

1. -------------------------------
2.   Version 2.05  bla bla
3.   bla bla
4.   bla
5.
6.    ------------------------------
7.    xx                    0.0   4.5 6.7
8.    xx (yy)               2.0   4.5 5.6
9.    a/b/c/d ()            6.0    2.0 3.4
10.  a/b/c/d (yy)
11.
12.  h/j/k/l/m             5.0 9.0 8.9
13.  h/j/k/l/m ()
14.
15.  xx                    0.0    0.0   0.0
16.  yy                    8.9 8.9 8.0
17. ---------------------------------------


Thank you for your time & help
Vandana
D7908f05c89e965f6bc5308ad6f41256?d=identicon&s=25 Siep Korteling (steenslag)
on 2008-10-25 00:06
Vandana wrote:
> Hi all,
>
> I'm fairly new to programming with regular expressions.  I would like
> some help with the
> following. Consider the format given below as what I will receive in a
> file.
>
> While reading from the file, Im interested only in reading those lines
> that have the forward-slash' in
> them (lines 9,10,12, 13). How can I do this in ruby?

(...)

You don't need a regular expression for this. To select the lines which
include a slash, you can do

file = File.new( 'D:/1.txt' )
slashlines = file.select {|line| line.include?( '/' )}
file.close
puts slashlines


The select method will read each line and put each line for which the
block is true in an array.

hth,

Siep
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2008-10-25 01:13
(Received via mailing list)
On Fri, Oct 24, 2008 at 6:05 PM, Siep Korteling
<s.korteling@gmail.com>wrote:

> block is true in an array.
Siep, I can't find any documentation on File#select, and IO#select is
not
documented as doing what you show. Could you provide more info about it?

Thanks,
Craig
D337e2cf7c6d03caac6988d83e0e7f66?d=identicon&s=25 Michael Guterl (mguterl)
on 2008-10-25 01:17
(Received via mailing list)
On Fri, Oct 24, 2008 at 7:12 PM, Craig Demyanovich
<cdemyanovich@gmail.com> wrote:
>>
>> The select method will read each line and put each line for which the
>> block is true in an array.
>
>
> Siep, I can't find any documentation on File#select, and IO#select is not
> documented as doing what you show. Could you provide more info about it?
>
It is a method from Enumerable.

ri Enumerable#select

Michael Guterl
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2008-10-25 01:23
(Received via mailing list)
On Fri, Oct 24, 2008 at 7:17 PM, Michael Guterl <mguterl@gmail.com>
wrote:

> It is a method from Enumerable.
>
> ri Enumerable#select


D'oh, didn't think of Enumerable for some reason. Time to learn more
about
file I/O.

Thanks,
Craig
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2008-10-25 01:33
(Received via mailing list)
On Fri, Oct 24, 2008 at 6:12 PM, Craig Demyanovich
<cdemyanovich@gmail.com> wrote:
>>
>> The select method will read each line and put each line for which the
>> block is true in an array.
>
>
> Siep, I can't find any documentation on File#select, and IO#select is not
> documented as doing what you show. Could you provide more info about it?

I couldn't find it explicitly in the docks either, but if you do
File.ancestors, you will see that Enumerable is in there.

To the OP, you could also do (for regex)...

slashlines = my_file.select {|line| line =~ /\//}


Todd
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2008-10-25 02:55
(Received via mailing list)
Vandana wrote:

>
> 11.
> 12.  h/j/k/l/m             5.0 9.0 8.9
> 13.  h/j/k/l/m ()
> 14.
> 15.  xx                    0.0    0.0   0.0
> 16.  yy                    8.9 8.9 8.0
> 17. ---------------------------------------
>
>
> Thank you for your time & help
> Vandana

Awk:

awk "/\//" myfile


Ruby:

ruby -ne "print if /\//" myfile
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2008-10-25 04:52
(Received via mailing list)
Vandana wrote:
> While reading from the file, Im interested only in reading those lines
> that have the forward-slash' in
> them (lines 9,10,12, 13). How can I do this in ruby?

file.grep %r(/)
or
file.grep /\//

HTH,
Sebastian
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2008-10-25 07:58
(Received via mailing list)
From: Todd Benson [mailto:caduceass@gmail.com]
# I couldn't find it explicitly in the docks either, but if you do
# File.ancestors, you will see that Enumerable is in there.


qri can find it

botp@botp-desktop:~$ qri File#select
------------------------------------------------------ Enumerable#select
     enum.find_all {| obj | block }  => array
     enum.select   {| obj | block }  => array
------------------------------------------------------------------------
     Returns an array containing all elements of enum for which block
     is not false (see also Enumerable#reject).

        (1..10).find_all {|i|  i % 3 == 0 }   #=> [3, 6, 9]
0c773c65707ec2ff412d05b7559af343?d=identicon&s=25 Vandana (Guest)
on 2008-10-27 21:30
(Received via mailing list)
Thanks to all!

I used f.grep(/\//) to read the lines that were needed.

Thanks once more.
> From: Todd Benson [mailto:caduce...@gmail.com]
This topic is locked and can not be replied to.