Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > regex problem with gsub

Reply
Thread Tools

regex problem with gsub

 
 
Gilbert Rebhan
Guest
Posts: n/a
 
      09-22-2007
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



 
Reply With Quote
 
 
 
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      09-22-2007
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
 
 
 
Gilbert Rebhan
Guest
Posts: n/a
 
      09-22-2007
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
 
Reply With Quote
 
Jovino
Guest
Posts: n/a
 
      09-22-2007
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 [(E-Mail Removed)]=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





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
regex: \<start-of-word, and end-of-word\> not in gsub? Shea Martin Ruby 1 01-15-2007 08:15 PM
Crazy gsub/regex scheme - can this be done better? Wes Gamble Ruby 3 08-11-2006 11:49 PM
gsub and gsub! are inconsistent aurelianito Ruby 9 11-09-2005 01:38 PM
Regex back reference in gsub Dan Fitzpatrick Ruby 4 07-13-2005 10:31 PM



Advertisments