Using Regex in Ruby to parse out a chunk of file names -


i trying standardize file names in directory have similarities, not consistent. are, however, standard enough.

examples of file names (where date month/day/year):

  • weekly sales report 022213 lv.xls
  • weekly sales report 091908 lv-f.xls
  • weekly sales 072508.xls
  • weekly u s sales v1.0 061308.xls
  • weekly u.s. sales jan0606.xls

my current solution has been effective, ugly find , replace possible string combinations. x.gsub!(/^weekly|sales|report|u s|u.s.|\s/,'')

however, assume there way @ file name string , grab chunk has of date information. chunk bounded whitespace on left , ends in @ least 4 digits. there straightforward way accomplish this?

your requirement stated suggest following:

date_portion = x.match(/\s(\s*\d{4,8})/)[1] 

that's: match 1 whitespace char, capture zero-or-more non-whitespace, followed 4 8 digits; return captured text.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -