# Why does . match non-ascii chars?

**URL:** <https://www.ruby-forum.com/t/why-does-match-non-ascii-chars/161125>\
**Category:** Ruby\
**Created:** [February 23, 2009, 5:32pm UTC](https://www.ruby-forum.com/t/why-does-match-non-ascii-chars/161125 "2009-02-23T17:32:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [February 23, 2009, 5:32pm UTC](https://www.ruby-forum.com/t/why-does-match-non-ascii-chars/161125/1 "2009-02-23T17:32:57Z")

</div>

str = "abcdÃ©f "

result = str.gsub(/./n) do |match|  
puts “%%%02X” % match[0]  
end  
puts

–output:–  
%61  
%62  
%63  
%64  
%C3  
%A9  
%66

Doesn’t the ‘n’ option say to match ascii? For what it’s worth, I get  
the same result without the ‘n’ option.

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [February 24, 2009, 2:16am UTC](https://www.ruby-forum.com/t/why-does-match-non-ascii-chars/161125/2 "2009-02-24T02:16:11Z")

</div>

On Tue, Feb 24, 2009 at 1:34 AM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> %62  
> %63  
> %64  
> %C3  
> %A9  
> %66
> 
> Doesn’t the ‘n’ option say to match ascii? For what it’s worth, I get  
> the same result without the ‘n’ option.

The default switch of a regex is actually ‘n’ already, that only  
changes if you set $KCODE before.  
It has little influence on what is matched when it comes to ‘.’, but  
it influences how the matched bytes will be grouped to resemble  
characters.

sigma ~ % ruby -e ‘p "abcdÃ©f ".scan(/./)’  
[“a”, “b”, “c”, “d”, “\303”, “\251”, “f”, " "]

sigma ~ % ruby -e ‘p "abcdÃ©f ".scan(/./u)’  
[“a”, “b”, “c”, “d”, “\303\251”, “f”, " "]

sigma ~ % ruby -Kue ‘p "abcdÃ©f ".scan(/./u)’  
[“a”, “b”, “c”, “d”, “Ã©”, “f”, " "]

sigma ~ % ruby19 -e ‘p "abcdÃ©f ".scan(/./)’  
[“a”, “b”, “c”, “d”, “Ã©”, “f”, " "]

Please see some excellent articles about this topic from James Edward  
Gray II:

[http://blog.grayproductions.net/articles/bytes\_and\_characters\_in\_ruby\_18](http://blog.grayproductions.net/articles/bytes_and_characters_in_ruby_18)  
[http://blog.grayproductions.net/categories/character\_encodings](http://blog.grayproductions.net/categories/character_encodings)

^ manveru
