regex problem with gsub

G

Gilbert Rebhan

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
 
S

Sebastian Hungerecker

Gilbert said:
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
 
G

Gilbert Rebhan

Hi,

Sebastian said:
Gilbert said:
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
 
J

Jovino

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:[email protected]]=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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top