From: Robert Klemme Date: 2010-09-09T18:08:55+09:00 Subject: Re: Regex breaks when string contains binary data On Wed, Sep 8, 2010 at 1:29 PM, Aisha Fenton wrote: > $KCODE is set to "UTF8", and my RUBYOPT env variable is set to > "rubygems". > > Hmmm could be the problem. I haven't really got my head around Ruby's > String encoding, might be time. You can check the encoding of the string like this: irb(main):006:0> s="\345HI" => "\xE5HI" irb(main):007:0> s.encoding => # irb(main):008:0> s.encoding.to_s => "CP850" It's also interesting to see what byte length and character length you get. irb(main):009:0> s.length => 3 irb(main):010:0> s.bytesize => 3 If you get a value < 3 for character length that would explain the mismatch. Also, you can check contents of the subsequence that you think should match: irb(main):011:0> s[-2..-1] => "HI" irb(main):012:0> s[1..-1] => "HI" Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/