From: "Peña, Botp" Date: 2007-08-08T11:14:54+09:00 Subject: Re: Determining the common prefix for several strings From: Brian Candler [mailto:B.Candler@pobox.com] # The trouble with that is that it won't work properly if # #{prefix} contains regexp special characters, and it may even bomb out: # # irb(main):001:0> prefix = "abc[" # => "abc[" # irb(main):002:0> "abc[def" =~ /^#{prefix}/ # RegexpError: invalid regular expression; '[' can't be the # last character ie. can't start range at the end of pattern: /^abc[/ # from (irb):2 arggh, why didn't i think about that test case? :) Thanks for the enlightenment. # You can use Regexp::escape to fix that, but then you're still # building a regexp every time round the loop. That's why I think # String#index is better here. indeed, and index is very fast too. but it still cannot beat those pure regex solutions though. the regex solution is short but my feeble mind cannot decipher it yet on why it works best. maybe i wish regex expressions were as readable as ruby :) kind regards -botp