On Wed, Apr 23, 2008 at 5:48 AM, Clement Ow
<clement.ow / asia.bnpparibas.com> wrote:
> I have my code which looks like this:
>  >>   delete= 5 + 2 #escape counting in weekends i.e Sat,Sun
>  >>   folders = $del_path
>  >>    puts delete_date = DateTime.now - delete
>
>  >>    regexp = Regexp.compile(/(\d{4}\d{2}\d{2})/)
>
>  >>    fileData = Struct.new(:name, :size)
>  >>    deleted_files = []
>
>  >>   folders.each do |folder|
>  >>      Dir.glob(folder+"/*") do |file|
>  >>        match = regexp.match(File.basename(file));
>  >>        if match
>  >>          file_date = DateTime.parse(match[1])
>  When my file name is in the format, 20080331 for example, the script
>  will run successfully. However, if the filename has additional
>  characters added to it, say, risk20080331, it'll run an error. And i
>  reckon it's the cause of the above line.

Sorry, what is the error? Cause this works for me:

irb(main):001:0> regexp = Regexp.compile(/(\d{4}\d{2}\d{2})/)
=> /(\d{4}\d{2}\d{2})/
irb(main):002:0> match = regexp.match("risk20080331.log")
=> #<MatchData:0xb7ce20f4>
irb(main):003:0> match[1]
=> "20080331"
irb(main):005:0> require 'date'
=> true
irb(main):006:0> DateTime.parse(match[1])
=> #<DateTime: 4909113/2,0,2299161>

So any string that contains 4 digits followed by 2 digits followed by
2 digits will match that regexp,
independently of what it has around the numbers:

irb(main):007:0> regexp.match("12345678")[1]
=> "12345678"
irb(main):008:0> regexp.match("12345678asdfasdf")[1]
=> "12345678"
irb(main):009:0> regexp.match("asdfasdf12345678asdfasdf")[1]
=> "12345678"
irb(main):010:0> regexp.match("asdfasdf12345678")[1]
=> "12345678"

Jesus.