# Bug in regex engine ? Must be

**URL:** <https://www.ruby-forum.com/t/bug-in-regex-engine-must-be/130842>\
**Category:** Ruby\
**Created:** [March 3, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/bug-in-regex-engine-must-be/130842 "2008-03-03T20:24:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [March 3, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/bug-in-regex-engine-must-be/130842/1 "2008-03-03T20:24:02Z")

</div>

Hi,

I’m using Ruby 1.8.6, and I just discovered something rather  
interesting, here is a test:

require ‘test/unit’

class TestRegexBug \< Test::Unit::TestCase

def test\_bug

```
hours = "pon-Äet"

assert(hours =~ /[Ä]et/i)
assert(hours =~ /Äet/i)
assert(hours =~ /-Äet/i)
assert(hours =~ /[cÄ]et/i)
assert(hours =~ /-[Ä]et/i)

```

end

end

As you can see, this only happens with unicode letters… (the last test  
fails)… I’m used to the fact that //i doesn’t work for unicode chars  
and I already know that you need two dots to match one of these… But  
this problem is different and weirder, because what triggers it is a  
minus sign before the square brackets… if you remove either the ‘-’ or  
‘[]’ from the regex, it works…

Can you comment?

thank you,  
david

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [March 3, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/bug-in-regex-engine-must-be/130842/2 "2008-03-03T23:47:51Z")

</div>

On Mar 3, 2008, at 2:24 PM, D. Krmpotic wrote:

> Hi,
> 
> I’m using Ruby 1.8.6, and I just discovered something rather  
> interesting, here is a test:

$KCODE = ‘UTF8’  
require ‘jcode’

> assert(hours =~ /-Äet/i)  
> and I already know that you need two dots to match one of these… But  
> this problem is different and weirder, because what triggers it is a  
> minus sign before the square brackets… if you remove either the ‘-’  
> or  
> ‘[]’ from the regex, it works…
> 
> Can you comment?
> 
> thank you,  
> david

Ruby is not natively aware of unicode, but you can get all these to  
pass if you give it the $KCOCDE hint.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [March 7, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/bug-in-regex-engine-must-be/130842/3 "2008-03-07T22:30:46Z")

</div>

Great info… completely forgot that this is available…  
thank you  
david

> $KCODE = ‘UTF8’  
> require ‘jcode’

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [March 8, 2008, 1:42am UTC](https://www.ruby-forum.com/t/bug-in-regex-engine-must-be/130842/4 "2008-03-08T01:42:05Z")

</div>

2008/3/3, D. Krmpotic [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> end
> 
> As you can see, this only happens with unicode letters… (the last test  
> fails)… I’m used to the fact that //i doesn’t work for unicode chars  
> and I already know that you need two dots to match one of these… But  
> this problem is different and weirder, because what triggers it is a  
> minus sign before the square brackets… if you remove either the ‘-’ or  
> ‘[]’ from the regex, it works…

In the regex [è] is a character class with _two_ bytes. So  
Ruby tries to match a minus followed by _one_ of the bytes  
out of “è” followed by “et”. So the regex would match  
“pon-\304et” or “pon-\215et”, but not “pon-\304\215et”.

Stefan
