From: "Wolfgang Nádasi-Donner" Date: 2007-01-26T04:10:10+09:00 Subject: Re: Regexp and string halts ruby Ricardo Ramalho schrieb: > I've tried the following regexp/string match: > > /^((?:\s*.+[;=][^,]*,?)+)$/ =~ " pid=934, time=478611, > command=TMQFORWARD, args=-C dom=ccaApp -g 650 -i 4278 -u pthp68 -U > /home1/eclprp/logs/tx/ULOG -m 0 -- -i 10 -t 600 -q AW02,AW02S TMQFORWARD > -C dom=ccaApp -g 650 -i 4278 -u pthp68 -U /home1/eclprp/logs/tx/ULOG -m > 0 -- -i 10 -t 600 -q AW02, " > > On a Debian/Linux system, with ruby version 1.8.4(-7sarge5) and even on > the more recent 1.8.5(-4) ruby enters an infinite active loop, and never > returns. > > Can anyone confirm/deny this possible bug on different architectures? > Thanks. > There is no infinite loop - it it takes a long time, because your regular expression is possibly too complex. These "(...*...+...*...)+" constructs are dangerous. See "Mastering Regular Expressions - Friedl" for details. An example with your regular expression and your string, starting with a short part an taken longer ones each step. The whole string may take some hundred years on my small machine ;-) >>>>> Code >>>>> regex = /^((?:\s*.+[;=][^,]*,?)+)$/ string = " pid=934, time=478611,command=TMQFORWARD, args=-C dom=ccaApp -g 650 -i 4278 -u pthp68, " t = Time.new string.match(regex) puts "Match need appr. #{Time.now-t} seconds" STDOUT.flush string = " pid=934, time=478611,command=TMQFORWARD, args=-C dom=ccaApp -g 650 -i 4278 -u pthp68 -U /home1/eclprp/logs/tx/ULOG, " t = Time.new string.match(regex) puts "Match need appr. #{Time.now-t} seconds" STDOUT.flush string = " pid=934, time=478611,command=TMQFORWARD, args=-C dom=ccaApp -g 650 -i 4278 -u pthp68 -U /home1/eclprp/logs/tx/ULOG -m 0 -- -i 10 -t 600 -q AW02,AW02S TMQFORWARD, " t = Time.new string.match(regex) puts "Match need appr. #{Time.now-t} seconds" >>>>> Output >>>>> Match need appr. 7.0 seconds Match need appr. 21.38 seconds Match need appr. 60.658 seconds >>>>> EoE >>>>> Wolfgang Nádasi-Donner