Hi,
Is this another case of overlooking something obvious? (Besides the
obvious fact that there are better ways to do this.)
It is case3 below that causes the problem. Case1 and case2 are 2 variants
of case3 that differ as follows: In case1, the "*" underlined by
"...--X---..." was inserted (with the successful aim of making the
preceding RE match as much as possible rather than a character at a time,
yet still produce the same overall result), and in case2, the "(.*/)*"
underlined by "...++++++..." was deleted (which changes what the overall
RE matches in the general case, but which unexpectedly stopped the stack
overflow error).
==========================================
#!/usr/bin/env ruby
puts "==== Case 1 ===="
p1 = Regexp.new("^(.*/)*(((?:[^./]*|[.](?=[^./]*[.]))*)([.][^./]*)*)$")
# ++++++ -------X----------------
m1 = p1.match("myfile.txt.sav")
m1.to_a.each { |x| puts x }
puts "==== Case 2 ===="
p2 = Regexp.new("^(((?:[^./]|[.](?=[^./]*[.]))*)([.][^./]*)*)$")
# -----------------------
m2 = p2.match("myfile.txt.sav")
m2.to_a.each { |x| puts x }
puts "==== Case 3 ===="
p3 = Regexp.new("^(.*/)*(((?:[^./]|[.](?=[^./]*[.]))*)([.][^./]*)*)$")
# ++++++ -----------------------
m3 = p3.match("myfile.txt.sav")
m3.to_a.each { |x| puts x }
==========================================
produces this output:
==========================================
# ./parse_path.rb
==== Case 1 ====
myfile.txt.sav
nil
myfile.txt.sav
myfile.txt
.sav
==== Case 2 ====
myfile.txt.sav
myfile.txt.sav
myfile.txt
.sav
==== Case 3 ====
./parse_path.rb:18:in `match': Stack overflow in regexp matcher:
/^(.*\/)*(((?:[^.\/]|[.](?=[^.\/]*[.]))*)([.][^.\/]*)*)$/ (RegexpError)
from ./parse_path.rb:18
==========================================
using ruby 1.6.2 (2000-10-12) [rs6000-aix4.3.2.0].
However this doesn't fail for ruby 1.6.1 (2000-10-05) [i386-cygwin].
Conrad Schneiker
(This note is unofficial and subject to improvement without notice.)