From: Robert Klemme Date: 2009-06-25T20:07:28+09:00 Subject: Re: regex that works on rubular.com but not in my program 2009/6/25 Andreas Hansen > i have some trouble with a regex. > it works on rubular.com but not in my program > ive used the content in testfile.txt on rubular.com > > the regex finds a ip-address, a flag and a username in a TCP-packet(an > example: http://rubular.com/regexes/8389) > > regex = > /(?:[I][P]\s)((?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}|(?:\d{1,3}\.){3}\d{1,3}).{0,}(?:[:][\s])([P])(?:.{0,})$\s(?:^[E].{5}[@].{9}[Q])(?:.{30,42}\.)\W{0,}([\w\d]{1}(?:[\w\d-]+.){1,13}[\wA-Z0-9])/i Ugh! This is completely unreadable. How about using switch /x and embedding some comments? Constructing the large regexp from a few smaller expressions might also help. For example, you could use /[a-f0-9]/i for hex digits. Few notes on glancing over this [\wA-Z0-9] -> \w \w includes characters and numbers [\s] -> \s [P] -> P etc. [\d\d]{1} -> \w > filename = 'testfile.txt' > file = File.open(filename).collect Not closing the file descriptor properly... > j = file.length > i = 0 > while  i< j > >  a = file[i].to_s > >  b = a.scan(regex) >  print b.length You are not printing a newline here. Are you maybe missing the print output? >  i = i + 1 > end You can greatly simplify your code to File.foreach filename do |line| b = line.scan rx puts b.length end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/