Regexp.match(line) question

Discussion in 'Ruby' started by m b, Jun 13, 2011.

  1. m b

    m b Guest

    Hi=2C
    I new to Ruby. It's a lot of fun to learn..

    if I do this:

    f=3Dopen('filename.dat'=2C'r')
    str=3Df.read

    and then:

    str.each_line do |line|
    /ruby/.match(line)

    ruby prints out variable line and say its a RegexpError
    'empty range in character class' as if it interprets line as a Regexp
    What am I doing wrong? There are a lot of strange characters in variable l=
    ine=2C
    maybe that is the issue?
    =
     
    m b, Jun 13, 2011
    #1
    1. Advertising

  2. Re: Regexp.match(line) question

    On Mon, Jun 13, 2011 at 7:32 PM, m b <> wrote:
    >
    > Hi,
    > I new to Ruby. =A0It's a lot of fun to learn..
    >
    > if I do this:
    >
    > f=3Dopen('filename.dat','r')
    > str=3Df.read
    >
    > and then:
    >
    > str.each_line =A0do |line|
    > =A0 =A0/ruby/.match(line)
    >
    > ruby prints out variable line and say its a RegexpError
    > 'empty range in character class' =A0as if it interprets =A0line as a Rege=

    xp
    > What am I doing wrong? =A0There are a lot of strange characters in variab=

    le line,
    > maybe that is the issue?


    I'm not sure about the issue, but the usual idiom for this is:

    File.foreach("filename.dat") do |line|
    line.match(/ruby/)
    end

    This way:
    - The file handler is automatically closed for you, you don't have to
    ensure that you call close on it, it's done for you.
    - You only have one line at a time in memory, since that seems to be
    your unit of data. If the file is huge it will make a difference in
    the amount of memory you will consume at a time.

    In any case, String#match calls Regexp#match, so calling match on one
    or the other should make no difference. Can you show a small example
    of a file that breaks?

    Jesus.
     
    Jesús Gabriel y Galán, Jun 13, 2011
    #2
    1. Advertising

  3. m b

    m b Guest

    Re: Regexp.match(line) question


    > > if I do this:
    > >
    > > f=3Dopen('filename.dat'=2C'r')
    > > str=3Df.read
    > >
    > > and then:
    > >
    > > str.each_line do |line|
    > > /ruby/.match(line)
    > >
    > > ruby prints out variable line and say its a RegexpError
    > > 'empty range in character class' as if it interprets line as a Regexp
    > > What am I doing wrong? There are a lot of strange characters in variab=

    le line=2C
    > > maybe that is the issue?

    >=20
    > I'm not sure about the issue=2C but the usual idiom for this is:
    >=20
    > File.foreach("filename.dat") do |line|
    > line.match(/ruby/)
    > end
    >=20
    > This way:
    > - The file handler is automatically closed for you=2C you don't have to
    > ensure that you call close on it=2C it's done for you.
    > - You only have one line at a time in memory=2C since that seems to be
    > your unit of data. If the file is huge it will make a difference in
    > the amount of memory you will consume at a time.
    >=20
    > In any case=2C String#match calls Regexp#match=2C so calling match on one
    > or the other should make no difference. Can you show a small example
    > of a file that breaks?
    >=20
    > Jesus.



    Aha.. Thats a better way to do it.. File.foreach Thanx.



    the string-variable line both starts and and ends with a '/' . Its =
    a series of paths from a linux system hd =20

    But its a String to be searched and not a Regexp




    =
     
    m b, Jun 13, 2011
    #3
  4. m b

    m b Guest

    Re: Regexp.match(line) question


    > > > if I do this:
    > > >
    > > > f=3Dopen('filename.dat'=2C'r')
    > > > str=3Df.read
    > > >
    > > > and then:
    > > >
    > > > str.each_line do |line|
    > > > /ruby/.match(line)
    > > >
    > > > ruby prints out variable line and say its a RegexpError
    > > > 'empty range in character class' as if it interprets line as a Rege=

    xp
    > > > What am I doing wrong? There are a lot of strange characters in vari=

    able line=2C
    > > > maybe that is the issue?


    I found out what I was doing the wrong way..

    I'm reading the search pattern with readline
    and I wrote it -->/ruby/ =20
    That didnt make it a Regexp.
    Now I found that if I write it -->ruby
    and then
    pattern=3DRegexp.new(input_str) it works..

    /Mix


    =
     
    m b, Jun 13, 2011
    #4
  5. Re: Regexp.match(line) question

    On Mon, Jun 13, 2011 at 10:26 PM, m b <> wrote:
    >
    >
    >
    >> > > if I do this:
    >> > >
    >> > > f=3Dopen('filename.dat','r')
    >> > > str=3Df.read
    >> > >
    >> > > and then:
    >> > >
    >> > > str.each_line =A0do |line|
    >> > > =A0 =A0/ruby/.match(line)
    >> > >
    >> > > ruby prints out variable line and say its a RegexpError
    >> > > 'empty range in character class' =A0as if it interprets =A0line as a=

    Regexp
    >> > > What am I doing wrong? =A0There are a lot of strange characters in v=

    ariable line,
    >> > > maybe that is the issue?


    You must have a different regular expression in your test other than
    /ruby/ because that is valid:

    irb(main):001:0> m =3D /ruby/.match "text"
    =3D> nil
    irb(main):002:0> m =3D /ruby/.match "ruby"
    =3D> #<MatchData "ruby">

    > I found out what I was doing the wrong way..
    >
    > I'm reading the search pattern with =A0readline
    > and I wrote it -->/ruby/
    > That didnt make it a Regexp.


    This is not true. The sequence /ruby/ *is* a valid regular expression.

    10:01:30 ~$ ruby19 -e 'p /ruby/'
    /ruby/
    10:01:38 ~$ ruby19 -e 'p /ruby/.class'
    Regexp
    10:01:42 ~$

    There must be something else going on. Please post your _complete_
    code you actually have.

    Kind regards

    robert


    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Jun 14, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mikel Lindsaar
    Replies:
    0
    Views:
    542
    Mikel Lindsaar
    Mar 31, 2008
  2. Old Echo
    Replies:
    1
    Views:
    200
    Adam Shelly
    Sep 4, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    404
    7stud --
    Aug 21, 2009
  4. Match line by line

    , Jun 5, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    94
  5. Uldis  Bojars
    Replies:
    2
    Views:
    213
    Janwillem Borleffs
    Dec 17, 2006
Loading...

Share This Page