From: Ian Macdonald Date: 2007-02-15T00:17:37+09:00 Subject: Re: Puzzling regex behaviour On Wed 14 Feb 2007 at 10:25:10 +0900, Rob Biedenharn wrote: > I'm beginning to wonder if the original question is even accurate. > Doing nothing more than changing the encoding and re-saving the file > (where the value for foo was a cut-n-paste from the email), there > doesn't seem to be any discrpeancy between ruby and irb. (This > output is from ruby 1.8.5, but 1.8.2 was the same) > > rab:code/ruby $ file regexp_and_alnum_versus_w.rb > regexp_and_alnum_versus_w.rb: ISO-8859 text > rab:code/ruby $ cat regexp_and_alnum_versus_w.rb > foo = "pr?f?r?es" > alnum = /[^[:alnum:]]/ > dubya = /\W/ > > puts "foo\n => #{foo.inspect}" > [ alnum, dubya ].each do |re| > puts "foo =~ #{re}\n => #{foo =~ re}" > end > rab:code/ruby $ ruby regexp_and_alnum_versus_w.rb > foo > => "pr\351f\351r\351es" > foo =~ (?-mix:[^[:alnum:]]) > => 2 What is your locale? I strongly suspect it's either unset or set to C. In those cases, I get the same results as you. If you use en_US or nl_NL, you'll find (or at least, I find) that 'foo =~ /[^[:alnum:]]/' returns nil in irb and 2 from a stand-alone script. In fact, even irb returns a different value from the command line. This is bizarre: $ irb -f < foo2 foo = "pr�f�r�es" "pr\351f\351r\351es" foo =~ /[^[:alnum:]]/ 2 foo =~ /\W/ 2 $ irb irb(main):001:0> foo = "pr�f�r�es" => "pr\351f\351r\351es" irb(main):002:0> foo =~ /[^[:alnum:]]/ => nil irb(main):003:0> foo =~ /\W/ => 2 As you can see, interactively irb returns nil for that first regex match. Ian -- Ian Macdonald | The cost of living hasn't affected its ian@caliban.org | popularity. http://www.caliban.org/ | | |