regex problem with gsub

Discussion in 'Ruby' started by Gilbert Rebhan, Sep 22, 2007.

  1. Hi,

    i have a dir like that and need the
    first part of the first filename =

    J:\test>dir /B
    20070921_0001.xml
    20070921_0002.xml
    20070921_0003.xml

    i tried with =

    str=File.basename(Dir["J:/test/*.xml"].sort[0]).gsub(/(\w{8}).+/, $1)

    but =
    TypeError: can't convert nil into String
    from (irb):4:in `gsub'
    from (irb):4
    from :0

    if i use =
    irb(main):005:0> str=File.basename(Dir["J:/test/*.xml"].sort[0])
    => "20070921_0001.xml"
    i get the filename

    how to change my gsub part to make it work, i want
    the first part of the filename, i.e.
    filename "20070921_0001.xml"
    i want "20070921"

    Regards, Gilbert
     
    Gilbert Rebhan, Sep 22, 2007
    #1
    1. Advertising

  2. Gilbert Rebhan wrote:
    > str=File.basename(Dir["J:/test/*.xml"].sort[0]).gsub(/(\w{8}).+/, $1)


    At this point $1 is nil, which is why you get the error. You have to keep in
    mind that arguments are evaluated before the method is called and $1 will
    only have value after that. You need to use '\1' instead of $1.


    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Sep 22, 2007
    #2
    1. Advertising

  3. Hi,

    Sebastian Hungerecker wrote:
    > Gilbert Rebhan wrote:
    >> str=File.basename(Dir["J:/test/*.xml"].sort[0]).gsub(/(\w{8}).+/, $1)

    >
    > At this point $1 is nil, which is why you get the error. You have to keep in
    > mind that arguments are evaluated before the method is called and $1 will
    > only have value after that. You need to use '\1' instead of $1.


    Thanks!!
    Works now. I'm still not sure, when to use \1 and when to use $1

    Regards, Gilbert
     
    Gilbert Rebhan, Sep 22, 2007
    #3
  4. Gilbert Rebhan

    Jovino Guest

    Hi Gilbert:

    The problem it's not the regex, it's the second parameter, try instead:

    string.gsub(/(\w{7}).+/, '\1')


    This supposing you want ever the first seven chars, if you want the
    characters before because they can be variable, try this regex:

    string.gsub(/(\w*)(_.*)*\..+/, '\1')

    I'm not mastered the regular expressions, any corrections appreciated.

    Regards,

    Jovino

    -----Mensaje original-----
    De: Gilbert Rebhan [mailto:]=20
    Enviado el: s=E1bado, 22 de septiembre de 2007 11:55
    Para: ruby-talk ML
    Asunto: regex problem with gsub

    Hi,

    i have a dir like that and need the
    first part of the first filename =3D

    J:\test>dir /B
    20070921_0001.xml
    20070921_0002.xml
    20070921_0003.xml

    i tried with =3D

    str=3DFile.basename(Dir["J:/test/*.xml"].sort[0]).gsub(/(\w{8}).+/, $1)

    but =3D
    TypeError: can't convert nil into String
    from (irb):4:in `gsub'
    from (irb):4
    from :0

    if i use =3D
    irb(main):005:0> str=3DFile.basename(Dir["J:/test/*.xml"].sort[0])
    =3D> "20070921_0001.xml"
    i get the filename

    how to change my gsub part to make it work, i want
    the first part of the filename, i.e.
    filename "20070921_0001.xml"
    i want "20070921"

    Regards, Gilbert
     
    Jovino, Sep 22, 2007
    #4
    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. Dan Fitzpatrick

    Regex back reference in gsub

    Dan Fitzpatrick, Jul 13, 2005, in forum: Ruby
    Replies:
    4
    Views:
    381
    David A. Black
    Jul 13, 2005
  2. aurelianito

    gsub and gsub! are inconsistent

    aurelianito, Nov 8, 2005, in forum: Ruby
    Replies:
    9
    Views:
    167
    Robert Klemme
    Nov 9, 2005
  3. Wes Gamble
    Replies:
    3
    Views:
    179
    Wes Gamble
    Aug 12, 2006
  4. Shea Martin
    Replies:
    1
    Views:
    176
    Rodrigo Bermejo
    Jan 15, 2007
  5. Tom Cloyd

    gsub and regex

    Tom Cloyd, Jun 18, 2008, in forum: Ruby
    Replies:
    2
    Views:
    88
    Tom Cloyd
    Jun 18, 2008
Loading...

Share This Page