From: Luiz Angelo Daros de Luca Date: 2011-07-23T04:13:35+09:00 Subject: Re: Regexp inconsistency --00504502bf4fd91f1504a8ad42a5 Content-Type: text/plain; charset=UTF-8 Hello Camargo, Ruby is right. The regexp matches the string. $ ruby -e 'p (/ (\w+(\-\w+)?) \. (log|txt) [\.\-]? (\d{1}) ( \. (gz|bz2))? /x).match("net-snmpd.log-20110629.bz2")' # If you need that the whole string should match, and not part of it, you need to end your regexp with "$", like: / (\w+(\-\w+)?) \. (log|txt) [\.\-]? (\d{1}) ( \. (gz|bz2))? $/x Also, I recommend that you start your regexp with "^" /^ (\w+(\-\w+)?) \. (log|txt) [\.\-]? (\d{1}) ( \. (gz|bz2))? $/x This one should do the job. Regards, --- Luiz Angelo Daros de Luca, Me. luizluca@gmail.com 2011/7/22 Carlos Camargo > I observed a inconsistent behavior between ruby (ruby 1.8.6 (2009-06-08 > patchlevel 369) [x86_64-linux]), running in my host, and ruby in > www.rubular.com (reports "Rubular runs on Ruby 1.8.7."). The expression: > / (\w+(\-\w+)?) \. (log|txt) [\.\-]? (\d{1}) ( \. (gz|bz2))? /x > in my host returns (among others): > net-snmpd.log-20110629.bz2 > > In rubular site, recognize (correctly, I suppose!) just until: > net-snmpd.log-2 (stop here, i.e. don't recognize the string). > > Thanks for any help! (Upgrade ruby to 1.9 isn't an option!) > Camargo > > NB: My code (just for test) is: > #! /usr/bin/ruby -w > > require 'find' > > re_logdir = Regexp.new(/\/log$|\/log\/|\/logs$|\/logs\//) > dias = 5 > > ### > a='(\w+(\-\w+)?)' > d='(\d{4}[\.\-]?\d{2}[\.\-]?\d{2})' > h='(\d{2}[\.\-]?\d{2}[\.\-]?\d{2,6})' > l='(log|txt)' > z='(gz|bz2)' > n1='(\d{1})' > s='[\.\-]' > > re_logfile = Regexp.new(/ #{a} \. #{l} #{s}? #{n1} ( \. #{z})? /x ) > p re_logfile > > Find.find('/') do |f| > if File.file?(f) and > re_logdir.match( File.dirname(f) ) and > re_logfile.match( File.basename(f) ) > p f > end > end > > -- > Posted via http://www.ruby-forum.com/. > > --00504502bf4fd91f1504a8ad42a5--