# Regex.escape bug

**URL:** <https://www.ruby-forum.com/t/regex-escape-bug/190818>\
**Category:** Ruby\
**Created:** [June 28, 2010, 7:31pm UTC](https://www.ruby-forum.com/t/regex-escape-bug/190818 "2010-06-28T19:31:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zapotek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/3be4f8/32.png) [@zapotek](https://www.ruby-forum.com/u/zapotek)\
**Post date:** [June 28, 2010, 7:31pm UTC](https://www.ruby-forum.com/t/regex-escape-bug/190818/1 "2010-06-28T19:31:35Z")

</div>

Hi guys,

I’m using “ruby 1.9.1p378 (2010-01-10 revision 26273) [x86\_64-linux]”  
and just noticed a possible bug in Regex.escape() while using it with  
gsub()

## This:

## pp Regexp.escape( ‘.rb’ )

## yields:

## “\.rb”

while it should be “.rb”.

Regexp.new( ‘.rb’ ) or Regexp.new( ‘.rb’ ) work though, they replaces  
the “.rb” string as expected.  
Yes I know that “Regexp.new( ‘.rb’ )” works incidentally but I’m just  
mentioning it. 🙂

---

<div class="post-metadata">

**Author:** ![zapotek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/3be4f8/32.png) [@zapotek](https://www.ruby-forum.com/u/zapotek)\
**Post date:** [June 28, 2010, 8:53pm UTC](https://www.ruby-forum.com/t/regex-escape-bug/190818/2 "2010-06-28T20:53:52Z")

</div>

Tasos L. wrote:

> * * *
> 
> Yes I know that “Regexp.new( ‘.rb’ )” works incidentally but I’m just  
> mentioning it. 🙂

It is correct, because it lets you do this:

> > dot\_rb = Regexp.escape( ‘.rb’ )  
> > =\> “\.rb”
> 
> > /file#{dot\_rb}/ =~ “file.rb”  
> > =\> 0
> 
> > /file#{dot\_rb}/ =~ “fileQrb”  
> > =\> nil

Note that “\.rb” is the same string as ‘.rb’.

---

<div class="post-metadata">

**Author:** ![zapotek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/3be4f8/32.png) [@zapotek](https://www.ruby-forum.com/u/zapotek)\
**Post date:** [June 28, 2010, 9:55pm UTC](https://www.ruby-forum.com/t/regex-escape-bug/190818/3 "2010-06-28T21:55:22Z")

</div>

On 28.06.2010 19:31, Tasos L. wrote:

> * * *
> 
> ## yields:
> 
> ## “\.rb”
> 
> while it should be “.rb”.

No, it should be as it is as Joel explained. Note also

irb(main):007:0\> puts Regexp.escape( ‘.rb’ )  
.rb

It is the representation of the string that includes a backslash to  
properly escape the backslash:

irb(main):010:0\> “\n”.size  
=\> 2  
irb(main):011:0\> “\n”.size  
=\> 1  
irb(main):012:0\> puts “\n”  
\n  
=\> nil  
irb(main):013:0\> puts “\n”

=\> nil

> Regexp.new( ‘.rb’ ) or Regexp.new( ‘.rb’ ) work though, they replaces  
> the “.rb” string as expected.  
> Yes I know that “Regexp.new( ‘.rb’ )” works incidentally but I’m just  
> mentioning it. 🙂

It works but it does not match the sequence “.rb” - rather it matches  
any character followed by “rb”:

irb(main):004:0\> r = Regexp.new( ‘.rb’ )  
=\> /.rb/  
irb(main):005:0\> r.match ‘.rb’  
=\> #\<MatchData “.rb”\>  
irb(main):006:0\> r.match ‘oooopsrb’  
=\> #\<MatchData “srb”\>

Kind regards

robert
