# Regexp problem /\[Ã©Ãª\]/ || /Ã©|Ãª/

**URL:** <https://www.ruby-forum.com/t/regexp-problem-a-a-a-a/162172>\
**Category:** Ruby\
**Created:** [March 6, 2009, 1:33pm UTC](https://www.ruby-forum.com/t/regexp-problem-a-a-a-a/162172 "2009-03-06T13:33:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonatas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jonatas](https://www.ruby-forum.com/u/jonatas)\
**Post date:** [March 6, 2009, 1:33pm UTC](https://www.ruby-forum.com/t/regexp-problem-a-a-a-a/162172/1 "2009-03-06T13:33:51Z")

</div>

Hi, I got a problem try to replace accentuated characters like:

> irb  
> irb(main):001:0\>  
> irb(main):002:0\* name = “FÃªnix”  
> =\> “F\303\252nix”  
> irb(main):003:0\> name.gsub(/[Ã©Ãª]/,‘e’)  
> =\> “Feenix”  
> irb(main):004:0\> name.gsub(/Ã©|Ãª/,‘e’)  
> =\> “Fenix”

What’s the difference between /[Ã©Ãª]/ and /Ã©|Ãª/ ?

ps: ruby -v  
ruby 1.8.6 (2007-09-24 patchlevel 111) [x86\_64-linux]

---

<div class="post-metadata">

**Author:** ![jonatas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jonatas](https://www.ruby-forum.com/u/jonatas)\
**Post date:** [March 6, 2009, 4:37pm UTC](https://www.ruby-forum.com/t/regexp-problem-a-a-a-a/162172/2 "2009-03-06T16:37:51Z")

</div>

> > What’s the difference between /[éê]/ and /é|ê/ ?
> 
> In that context there shouldn’t be any difference

If the source is in utf-8, then ruby 1.8 interpretes [éê] as a choice  
of 4 bytes: [195, 169, 195, 170]

Fênix is seen as:  
[70, 195, 170, 110, 105, 120]

195 & 170 get replaced with “e”, hence Feenix.

---

<div class="post-metadata">

**Author:** ![jonatas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jonatas](https://www.ruby-forum.com/u/jonatas)\
**Post date:** [March 6, 2009, 3:15pm UTC](https://www.ruby-forum.com/t/regexp-problem-a-a-a-a/162172/3 "2009-03-06T15:15:29Z")

</div>

On Fri, Mar 6, 2009 at 6:02 PM, Jonatas P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi, I got a problem try to replace accentuated characters like:
> 
> irb(main):002:0\* name = “Fênix”  
> =\> “F\303\252nix”  
> irb(main):003:0\> name.gsub(/[éê]/,‘e’)  
> =\> “Feenix”  
> irb(main):004:0\> name.gsub(/é|ê/,‘e’)  
> =\> “Fenix”

Looks to me like an encoding problem. What source encoding are you  
working in?

If you set $KCODE = ‘UTF-8’ or append /u to the regex literals does it  
resolve the inconsistency?

> What’s the difference between /[éê]/ and /é|ê/ ?

In that context there shouldn’t be any difference. The union, |, can  
be used for patterns longer than a single character, but the specific  
patterns above look equivalent to me. But if the encoding isn’t set  
appropriately all bets are off!

> ps: ruby -v  
> ruby 1.8.6 (2007-09-24 patchlevel 111) [x86\_64-linux]

ps: the unicode support has apparently been much improved in 1.9.

Cheers,  
lasitha

---

<div class="post-metadata">

**Author:** ![jonatas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jonatas](https://www.ruby-forum.com/u/jonatas)\
**Post date:** [March 6, 2009, 6:29pm UTC](https://www.ruby-forum.com/t/regexp-problem-a-a-a-a/162172/4 "2009-03-06T18:29:29Z")

</div>

> If you set $KCODE = ‘UTF-8’ or append /u to the regex literals does it  
> resolve the inconsistency?

WORKS! setting $KCODE or using /u

interesting!!!

Thanks VERY MUCH!
