# Ruby Regexp implementation?

**URL:** <https://rubytalk.org/t/ruby-regexp-implementation/57750>\
**Category:** ruby-talk\
**Created:** [12 March 2010 18:54 UTC](https://rubytalk.org/t/ruby-regexp-implementation/57750 "2010-03-12T18:54:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aldric\_Giacomoni2](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@Aldric\_Giacomoni2](https://rubytalk.org/u/Aldric_Giacomoni2)\
**Post date:** [12 March 2010 18:54 UTC](https://rubytalk.org/t/ruby-regexp-implementation/57750/1 "2010-03-12T18:54:09Z")

</div>

This probably doesn't belong in Ruby Core. I was reading this article:  
[http://swtch.com/~rsc/regexp/regexp1.html](http://swtch.com/~rsc/regexp/regexp1.html)  
[http://swtch.com/~rsc/regexp/regexp2.html](http://swtch.com/~rsc/regexp/regexp2.html)  
[http://swtch.com/~rsc/regexp/regexp3.html](http://swtch.com/~rsc/regexp/regexp3.html)

And in the first few lines of the first page, it shows that the Regexp  
implementation for Perl is absolutely terrible, and it says that Ruby is  
"the same" or at least similar. Is Ruby 1.8 / 1.9 using the Thompson NFA  
matching? Will Ruby 2.0 use it?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Chuck\_Remes](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@Chuck\_Remes](https://rubytalk.org/u/Chuck_Remes)\
**Post date:** [12 March 2010 21:04 UTC](https://rubytalk.org/t/ruby-regexp-implementation/57750/2 "2010-03-12T21:04:24Z")

</div>

I remember reading that article when it was first published.

Ruby switched to a new regex library almost 2 years ago. Check out [http://rubyforge.org/projects/oniguruma](http://rubyforge.org/projects/oniguruma) for more information.

It would be interesting to see those benchmarks run again with these new implementations.

cr

> **···**
>
> On Mar 12, 2010, at 12:54 PM, Aldric Giacomoni wrote:
> 
> > This probably doesn't belong in Ruby Core. I was reading this article:  
> > [Regular Expression Matching Can Be Simple And Fast](http://swtch.com/~rsc/regexp/regexp1.html)  
> > [Regular Expression Matching: the Virtual Machine Approach](http://swtch.com/~rsc/regexp/regexp2.html)  
> > [Regular Expression Matching in the Wild](http://swtch.com/~rsc/regexp/regexp3.html)
> > 
> > And in the first few lines of the first page, it shows that the Regexp  
> > implementation for Perl is absolutely terrible, and it says that Ruby is  
> > "the same" or at least similar. Is Ruby 1.8 / 1.9 using the Thompson NFA  
> > matching? Will Ruby 2.0 use it?

---

<div class="post-metadata">

**Author:** ![Siep\_Korteling](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Siep\_Korteling](https://rubytalk.org/u/Siep_Korteling)\
**Post date:** [12 March 2010 21:14 UTC](https://rubytalk.org/t/ruby-regexp-implementation/57750/3 "2010-03-12T21:14:29Z")

</div>

Aldric Giacomoni wrote:

> This probably doesn't belong in Ruby Core. I was reading this article:  
> [Regular Expression Matching Can Be Simple And Fast](http://swtch.com/~rsc/regexp/regexp1.html)  
> [Regular Expression Matching: the Virtual Machine Approach](http://swtch.com/~rsc/regexp/regexp2.html)  
> [Regular Expression Matching in the Wild](http://swtch.com/~rsc/regexp/regexp3.html)
> 
> And in the first few lines of the first page, it shows that the Regexp  
> implementation for Perl is absolutely terrible, and it says that Ruby is  
> "the same" or at least similar. Is Ruby 1.8 / 1.9 using the Thompson NFA  
> matching? Will Ruby 2.0 use it?

def pathologicalize (str, n)  
&nbsp;&nbsp;tmp = str[0]\*n  
&nbsp;&nbsp;[str\*n + tmp, tmp]  
end

puts RUBY\_VERSION  
1.step(101, 10) do |n|  
&nbsp;&nbsp;full\_str, part\_str = pathologicalize("a?", n)  
&nbsp;&nbsp;start = Time.now  
&nbsp;&nbsp;1000.times do  
&nbsp;&nbsp;&nbsp;&nbsp;full\_str =~ /#{part\_str}/  
&nbsp;&nbsp;end  
&nbsp;&nbsp;duration = Time.now-start  
&nbsp;&nbsp;puts "#{n}:\t#{duration}"  
end

#output  
1.9.1  
1: 0.015625  
11: 0.015625  
21: 0.015625  
31: 0.03125  
41: 0.03125  
51: 0.046875  
61: 0.046875  
71: 0.046875  
81: 0.0625  
91: 0.0625  
101: 0.078125

Siep

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [12 March 2010 22:47 UTC](https://rubytalk.org/t/ruby-regexp-implementation/57750/4 "2010-03-12T22:47:21Z")

</div>

Hi,

> **···**
>
> In message "Re: Ruby Regexp implementation?" on Sat, 13 Mar 2010 03:54:09 +0900, Aldric Giacomoni \<aldric@trevoke.net\> writes:
> 
> > And in the first few lines of the first page, it shows that the Regexp  
> > implementation for Perl is absolutely terrible, and it says that Ruby is  
> > "the same" or at least similar. Is Ruby 1.8 / 1.9 using the Thompson NFA  
> > matching? Will Ruby 2.0 use it?
> 
> No plan. We need more (human) resource.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.
