# Ruby 1.9.1 - invalid multibyte escape: // (RegexpError)

**URL:** <https://www.ruby-forum.com/t/ruby-1-9-1-invalid-multibyte-escape-regexperror/164732>\
**Category:** Ruby\
**Created:** [April 4, 2009, 9:42pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-invalid-multibyte-escape-regexperror/164732 "2009-04-04T21:42:08Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 4, 2009, 9:42pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-invalid-multibyte-escape-regexperror/164732/1 "2009-04-04T21:42:08Z")

</div>

Hi, using TreeTop parser I had a grammar defined working in Ruby1.8 but  
it  
fails in 1.9.1:

~# ruby1.8 -e “Regexp.new(‘[\xC0-\xDF]’)”  
OK

~# ruby1.9 -e “Regexp.new(‘[\xC0-\xDF]’)”  
-e:1:in `initialize’: invalid multibyte escape: /[\xC0-\xDF]/  
(RegexpError)

I’ve found the following text about differences between 1.8 and 1.9:

“It is more rigorous that 1.8 when it comes to detecting invalid code.  
For  
example, 1.8 accepts /[^\x00-\xa0]/u, while 1.9 complains of invalid  
multibyte  
escape”

Ok, so how should I write the above Regexp to work on 1.9.1?

Thanks a lot.

–

IÃ±aki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 6, 2009, 7:43am UTC](https://www.ruby-forum.com/t/ruby-1-9-1-invalid-multibyte-escape-regexperror/164732/2 "2009-04-06T07:43:53Z")

</div>

On Apr 4, 2009, at 12:40, Iñaki Baz C. wrote:

> I’ve found the following text about differences between 1.8 and 1.9:
> 
> “It is more rigorous that 1.8 when it comes to detecting invalid  
> code. For  
> example, 1.8 accepts /[^\x00-\xa0]/u, while 1.9 complains of invalid  
> multibyte  
> escape”
> 
> Ok, so how should I write the above Regexp to work on 1.9.1?

Regexp.new ‘[\xC0-\xDF]’, nil, ‘n’

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 6, 2009, 10:04am UTC](https://www.ruby-forum.com/t/ruby-1-9-1-invalid-multibyte-escape-regexperror/164732/3 "2009-04-06T10:04:26Z")

</div>

2009/4/6 Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > Ok, so how should I write the above Regexp to work on 1.9.1?
> 
> Regexp.new ‘[\xC0-\xDF]’, nil, ‘n’

Great! Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 6, 2009, 10:06am UTC](https://www.ruby-forum.com/t/ruby-1-9-1-invalid-multibyte-escape-regexperror/164732/4 "2009-04-06T10:06:47Z")

</div>

2009/4/6 IÃ±aki Baz C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 2009/4/6 Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > > Ok, so how should I write the above Regexp to work on 1.9.1?
> > 
> > Regexp.new ‘[\xC0-\xDF]’, nil, ‘n’
> 
> Great! Thanks a lot.

However I don’t understant these parameters for Regexp.new.  
I read:  
[http://www.ruby-doc.org/core-1.9/classes/Regexp.html](http://www.ruby-doc.org/core-1.9/classes/Regexp.html)  
About the third parameter you use (‘n’) it doens’t appear on the doc Â¿?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 6, 2009, 4:36pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-invalid-multibyte-escape-regexperror/164732/5 "2009-04-06T16:36:59Z")

</div>

IÃ±aki Baz C. wrote:

> However I don’t understant these parameters for Regexp.new.  
> I read:  
> [http://www.ruby-doc.org/core-1.9/classes/Regexp.html](http://www.ruby-doc.org/core-1.9/classes/Regexp.html)  
> About the third parameter you use (‘n’) it doens’t appear on the doc Â¿?

All the new stuff to do with String and encodings in ruby 1.9 is  
undocumented.

(At least, it’s not documented within Ruby itself. You may be able to  
purchase a book which has some reverse-engineered documentation)

If you care about stability or documentation, my own advice is to stick  
with 1.8 - preferably 1.8.6.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 6, 2009, 4:48pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-invalid-multibyte-escape-regexperror/164732/6 "2009-04-06T16:48:46Z")

</div>

On Apr 6, 2009, at 9:36 AM, Brian C. wrote:

> Iñaki Baz C. wrote:
> 
> > However I don’t understant these parameters for Regexp.new.  
> > I read:  
> > [http://www.ruby-doc.org/core-1.9/classes/Regexp.html](http://www.ruby-doc.org/core-1.9/classes/Regexp.html)  
> > About the third parameter you use (‘n’) it doens’t appear on the  
> > doc ¿?
> 
> All the new stuff to do with String and encodings in ruby 1.9 is  
> undocumented.

I’ve got the majority of the new functionality covered in my m17n  
series now:

[http://blog.grayproductions.net/articles/understanding\_m17n](http://blog.grayproductions.net/articles/understanding_m17n)

I expect to have the minor side topics I’m still missing covered in  
the next few weeks.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 6, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-invalid-multibyte-escape-regexperror/164732/7 "2009-04-06T17:28:41Z")

</div>

2009/4/6 Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> purchase a book which has some reverse-engineered documentation)  
> Regexp.new of Ruby 1.9 is obviously documented in:  
> [http://www.ruby-doc.org/core-1.9/classes/Regexp.html](http://www.ruby-doc.org/core-1.9/classes/Regexp.html)  
> but the number of parameters doesn’t match with the reality Â¿?

Does it make sense? Isn’t that documentation been created with Rdoc?

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 6, 2009, 9:23pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-invalid-multibyte-escape-regexperror/164732/8 "2009-04-06T21:23:21Z")

</div>

IÃ±aki Baz C. wrote:

> 2009/4/6 Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > purchase a book which has some reverse-engineered documentation)  
> > Regexp.new of Ruby 1.9 is obviously documented in:  
> > [http://www.ruby-doc.org/core-1.9/classes/Regexp.html](http://www.ruby-doc.org/core-1.9/classes/Regexp.html)  
> > but the number of parameters doesn’t match with the reality Â¿?
> 
> Does it make sense? Isn’t that documentation been created with Rdoc?

The rdoc is only as good as the comments in the source code.

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 6, 2009, 5:06pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-invalid-multibyte-escape-regexperror/164732/9 "2009-04-06T17:06:57Z")

</div>

I’m working on documenting some of this stuff when I have time (always  
the magic words, eh? :-/). I ran dcov on the whole of Ruby core last  
week (results: [http://jeremymcanally.com/coverage.html](http://jeremymcanally.com/coverage.html) ; it’s a little  
deceiving since methods like to\_yaml I think are actually included  
from elsewhere. I’ll have to look…), and I’m currently setting up  
some tasks for myself to knock things out.

I might setup a Lighthouse for it or something if other people want to  
get involved.

–Jeremy

On Mon, Apr 6, 2009 at 9:36 AM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> purchase a book which has some reverse-engineered documentation)
> 
> ## If you care about stability or documentation, my own advice is to stick with 1.8 - preferably 1.8.6.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–

> **[Jeremy McAnally](http://jeremymcanally.com/)**

> **[entp - web incubator and consultancy](http://entp.com/)**

> **[OMG GIFs - Download & Share | GIFDB.com](https://gifdb.com/omg)**
>
> Browse 226 free omg GIFs. Download and share them on GIFDB.com — works in WhatsApp, Telegram, Discord, X and more.

My books:

> **[Ruby in Practice - Jeremy McAnally and Assaf Arkin](https://www.manning.com/books/ruby-in-practice)**
>
> Like Ruby itself, Ruby in Practice will make you more productive. The book shows you practical techniques and strategies for small projects and large-scale…

[http://humblelittlerubybook.com/](http://humblelittlerubybook.com/) (FREE!)

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 8, 2009, 4:50pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-invalid-multibyte-escape-regexperror/164732/10 "2009-04-08T16:50:54Z")

</div>

On Apr 8, 2009, at 3:03 AM, Brian C. wrote:

> - What about string literals which include escape sequences like \u?  
> This seems to override the source encoding rule.

I plan to cover this in the next article.

> - What encoding is chosen for regexp literals? (Seems to be different  
> rules to string literals). What about string literals which include  
> #{interpolation}? What about regexp literals which include  
> #{interpolation}?

I’m going to cover this too.

> - I think it will be worth explaining what you need to do to handle  
> binary data (using “rb” and “wb”, the ASCII-8BIT encoding, how to set  
> external encoding for STDIN, the fact that read() and gets() return  
> different encodings for the same data…)

Planned for the next article.

> - What actually happens if you use string operations on two strings  
> with  
> different encodings? e.g. str1 == str2, str1 + str2, str1 \<\< str2?  
> What  
> about indexing a hash with two strings which are identical byte  
> sequences but different encodings?

I feel a gave a much better strategy that prevents you from worrying  
about such things. However, that article did link to a detailed  
explanation.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 15, 2009, 8:32pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-invalid-multibyte-escape-regexperror/164732/11 "2009-04-15T20:32:50Z")

</div>

On Apr 8, 2009, at 9:47 AM, James G. wrote:

> > #{interpolation}?
> 
> I’m going to cover this too.
> 
> > - I think it will be worth explaining what you need to do to handle  
> > binary data (using “rb” and “wb”, the ASCII-8BIT encoding, how to set  
> > external encoding for STDIN, the fact that read() and gets() return  
> > different encodings for the same data…)
> 
> Planned for the next article.

I’ve added a new post to my m17n series covering all of the above and  
more:

[http://blog.grayproductions.net/articles/miscellaneous\_m17n\_details](http://blog.grayproductions.net/articles/miscellaneous_m17n_details)

James Edward G. II

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [April 8, 2009, 10:02am UTC](https://www.ruby-forum.com/t/ruby-1-9-1-invalid-multibyte-escape-regexperror/164732/12 "2009-04-08T10:02:53Z")

</div>

James G. wrote:

> I’ve got the majority of the new functionality covered in my m17n  
> series now:
> 
> [http://blog.grayproductions.net/articles/understanding\_m17n](http://blog.grayproductions.net/articles/understanding_m17n)
> 
> I expect to have the minor side topics I’m still missing covered in  
> the next few weeks.

This is a good start, but I think it just scratches the surface.

Questions which immediately spring to mind:

- What is the nature of the “compatible” relationship? Does A compatible  
with B imply B compatible with A? It’s not commutative:

irb(main):002:0\> a = “abc”.force\_encoding(“UTF-8”)  
=\> “abc”  
irb(main):003:0\> b = “def”.force\_encoding(“ISO-8859-1”)  
=\> “def”  
irb(main):004:0\> Encoding.compatible?(a,b)  
=\> #Encoding:UTF-8  
irb(main):005:0\> Encoding.compatible?(b,a)  
=\> #Encoding:ISO-8859-1

Also, it’s not encodings which are compatible, but actual strings. Two  
strings may or may not be compatible, dependent not just on their  
encoding, but on their actual content at that instant.

irb(main):006:0\> a = “abc\xff”.force\_encoding(“UTF-8”)  
=\> “abc\xFF”  
irb(main):007:0\> b = “def\xff”.force\_encoding(“ISO-8859-1”)  
=\> “defï¿½”  
irb(main):008:0\> Encoding.compatible?(a,b)  
=\> nil

- What about string literals which include escape sequences like \u?  
This seems to override the source encoding rule.

$ ruby19

# encoding: ISO-8859-1

puts “abc”.encoding  
puts “abc\u1234”.encoding  
^D  
ISO-8859-1  
UTF-8

- What encoding is chosen for regexp literals? (Seems to be different  
rules to string literals). What about string literals which include  
#{interpolation}? What about regexp literals which include  
#{interpolation}?

- What source encoding and external encoding is used in irb?

- I think it will be worth explaining what you need to do to handle  
binary data (using “rb” and “wb”, the ASCII-8BIT encoding, how to set  
external encoding for STDIN, the fact that read() and gets() return  
different encodings for the same data…)

- What actually happens if you use string operations on two strings with  
different encodings? e.g. str1 == str2, str1 + str2, str1 \<\< str2? What  
about indexing a hash with two strings which are identical byte  
sequences but different encodings?

- What do C extension writers need to know about strings? It seems at  
the moment there is some magic hidden state (ENC\_CODERANGE\_7BIT) which  
you must remember to update whenever you create or modify a string, and  
if you don’t, things break badly.  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/329267](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/329267)  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/23155](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/23155)

Regards,

Brian.
