# Issue with regular expressions and locales

**URL:** <https://www.ruby-forum.com/t/issue-with-regular-expressions-and-locales/188336>\
**Category:** Ruby\
**Created:** [May 10, 2010, 2:06pm UTC](https://www.ruby-forum.com/t/issue-with-regular-expressions-and-locales/188336 "2010-05-10T14:06:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![emili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/cc9497/32.png) [@emili](https://www.ruby-forum.com/u/emili)\
**Post date:** [May 10, 2010, 2:06pm UTC](https://www.ruby-forum.com/t/issue-with-regular-expressions-and-locales/188336/1 "2010-05-10T14:06:52Z")

</div>

Hi there!  
I’ve experienced a courious issue with regular expressions: my locale is  
es\_ES.utf-8 and I use gsub with regular expresisons to transform an  
article  
title in a permalink like this

permalink.gsub!(/\W/, ‘-’)

What’s the problem? In my local system I get this

“Â¿” =~ /\W/  
=\> 0

but in another systems with english locale I have this other result

“Â¿” =~ /\W/  
=\> nil

Is this a bug or regular expressions matching depends on system locale?

Thanks.

---

<div class="post-metadata">

**Author:** ![emili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/cc9497/32.png) [@emili](https://www.ruby-forum.com/u/emili)\
**Post date:** [May 10, 2010, 7:21pm UTC](https://www.ruby-forum.com/t/issue-with-regular-expressions-and-locales/188336/2 "2010-05-10T19:21:50Z")

</div>

> but in another systems with english locale I have this other result

Same version of Ruby?

See also [Win32 ruby1.9 regexp and cyrillic string - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/208777) perhaps.  
-r

---

<div class="post-metadata">

**Author:** ![emili](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/cc9497/32.png) [@emili](https://www.ruby-forum.com/u/emili)\
**Post date:** [May 10, 2010, 3:58pm UTC](https://www.ruby-forum.com/t/issue-with-regular-expressions-and-locales/188336/3 "2010-05-10T15:58:25Z")

</div>

Emili ParreÃ±o wrote:

> In my local system I get this
> 
> “Â¿” =~ /\W/  
> =\> 0
> 
> but in another systems with english locale I have this other result
> 
> “Â¿” =~ /\W/  
> =\> nil
> 
> Is this a bug or regular expressions matching depends on system locale?

(1) What exact version(s) of Ruby are you running? (Show  
RUBY\_DESCRIPTION constant). Behaviour varies between versions.

(2) What does

```
"Â¿".encoding

```

show on the two machines?

AFAIK the actual match should depend only on the encoding of the string,  
not the system locale in the environment - but if you find differently  
that would be of interest.

(3) It looks like you are doing this in IRB. IRB is not a good predictor  
of behaviour in ruby 1.9, since the encoding of string literals in IRB  
depends on the system locale - which is not true for ruby source code in  
source files.

So writing a small test .rb file and running that is probably better.

I’ve attempted to document what I’ve found so far about encoding  
behaviour in 1.9 here:

> <https://github.com/candlerb/string19/blob/master/string19.rb>
