# Is there some ruby equivalent of perl's /\\Q...\\E/?

**URL:** <https://www.ruby-forum.com/t/is-there-some-ruby-equivalent-of-perls-q-e/65113>\
**Category:** Ruby\
**Created:** [August 16, 2006, 10:55am UTC](https://www.ruby-forum.com/t/is-there-some-ruby-equivalent-of-perls-q-e/65113 "2006-08-16T10:55:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [August 16, 2006, 10:55am UTC](https://www.ruby-forum.com/t/is-there-some-ruby-equivalent-of-perls-q-e/65113/1 "2006-08-16T10:55:46Z")

</div>

Hello,

in Perl I have \Q and \E in regexps allowing me to “literally” insert  
variable containing special characters:

$ perl -e ‘$a=".\*"; print “a”=~/$a/ || 0, “a”=~/\Q$a\E/ || 0, “\n”’  
10  
$

i.e., for nonperlists, if I have '._’ string in the variable $a, then  
/$a/ is the same as /._/ (and as such matches ‘a’), but /\Q$a\E/ equals  
/.\*/ (and thus does not match ‘a’), because all special characters are  
treated as preceded by backslash.

Is there something like that in Ruby?

Anyway, why Ruby does not take over whole perl5 RE? Especially  
look-behind assertions and \L, \l like special characters?

Thanks,

P.

---

<div class="post-metadata">

**Author:** ![elgato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b487fb/32.png) [@elgato](https://www.ruby-forum.com/u/elgato)\
**Post date:** [August 16, 2006, 11:16am UTC](https://www.ruby-forum.com/t/is-there-some-ruby-equivalent-of-perls-q-e/65113/2 "2006-08-16T11:16:49Z")

</div>

On 15.08.2006 14:23, Pavel S. wrote:

> /$a/ is the same as /.\*/ (and as such matches ‘a’), but /\Q$a\E/ equals  
> /.\*/ (and thus does not match ‘a’), because all special characters are  
> treated as preceded by backslash.
> 
> Is there something like that in Ruby?

Not as elegant but possible:

11:10:15 [~]: irb  
irb(main):001:0\> Regexp.quote “.”  
=\> “\.”  
irb(main):002:0\> Regexp.escape “.”  
=\> “\.”  
irb(main):003:0\> /o#{Regexp.quote ‘\’}b/ =~ “foo\bar”  
=\> 2

Note, you may want to use /o modifier for improved efficiency.

> Anyway, why Ruby does not take over whole perl5 RE? Especially  
> look-behind assertions and \L, \l like special characters?

1.9 or 2.0 will have a new RX engine AFAIK.

```
robert
```
