# Ruby 1.9.1 Regexp Encoding Warnings

**URL:** <https://www.ruby-forum.com/t/ruby-1-9-1-regexp-encoding-warnings/178431>\
**Category:** Ruby\
**Created:** [November 4, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-regexp-encoding-warnings/178431 "2009-11-04T15:00:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [November 4, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/ruby-1-9-1-regexp-encoding-warnings/178431/1 "2009-11-04T15:00:17Z")

</div>

In the Mail gem I am doing Regexp’s on various strings that arrive,  
which can be basically anything (user defined). So I am dup’ing the  
string, and forcing the encoding to binary before I do the regexp…  
but I am getting this warning:

~/lib/mail/utilities.rb:56: warning: regexp match /…/n against to  
UTF-8 string

The code that produces that is:

```
aspecial = %Q|()<>[]:;.\\,"|
control = %Q|\x00-\x1f\x7f-\xff|
PHRASE_UNSAFE = /[#{Regexp.quote aspecial}#{control}]/n

def quote_phrase( str )
  if RUBY_VERSION >= '1.9'
    string = str.dup
    string.force_encoding('ASCII-8BIT')
    (PHRASE_UNSAFE === string) ? dquote(str) : str
  else
    (PHRASE_UNSAFE === str) ? dquote(str) : str
  end
end

```

Any clues?

The specs that generate these errors all pass, so the Regexp is doing  
what I intend it to do from what I can see.

---

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [November 5, 2009, 5:51am UTC](https://www.ruby-forum.com/t/ruby-1-9-1-regexp-encoding-warnings/178431/2 "2009-11-05T05:51:46Z")

</div>

On Thu, Nov 5, 2009 at 12:59 AM, Mikel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> In the Mail gem I am doing Regexp’s on various strings that arrive,  
> which can be basically anything (user defined). So I am dup’ing the  
> string, and forcing the encoding to binary before I do the regexp…  
> but I am getting this warning:
> 
> ~/lib/mail/utilities.rb:56: warning: regexp match /…/n against to UTF-8 string

Found the problem. Was a second regexp inside the first that was not  
properly encoded.

Mikel
