# Re: Beginner gsub and ri questions

**URL:** <https://www.ruby-forum.com/t/re-beginner-gsub-and-ri-questions/93988>\
**Category:** Ruby\
**Created:** [April 17, 2006, 6:38pm UTC](https://www.ruby-forum.com/t/re-beginner-gsub-and-ri-questions/93988 "2006-04-17T18:38:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [April 17, 2006, 6:38pm UTC](https://www.ruby-forum.com/t/re-beginner-gsub-and-ri-questions/93988/1 "2006-04-17T18:38:43Z")

</div>

Thanks for the response. Is there a way to do this without a regular  
expression? To further explain, I want the program to be able to do any  
kind of literal string replacement at runtime, except that case is  
ignored. If I use a regular expression as the match string, then  
certain characters could be interpreted in special, non-literal ways,  
which is not what I want in this case.

Jamal

---

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [April 17, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/re-beginner-gsub-and-ri-questions/93988/2 "2006-04-17T19:37:19Z")

</div>

I don’t fully understand the situation when a regex would work. Could  
you  
give me one?

-mark

---

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [April 17, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/re-beginner-gsub-and-ri-questions/93988/3 "2006-04-17T19:46:42Z")

</div>

On 4/17/06, Jamal M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for the response. Is there a way to do this without a regular  
> expression? To further explain, I want the program to be able to do any  
> kind of literal string replacement at runtime, except that case is  
> ignored. If I use a regular expression as the match string, then  
> certain characters could be interpreted in special, non-literal ways,  
> which is not what I want in this case.

What you want here is to construct your regular expression properly,  
such as:

catmatch = %r{#{Regexp::escape(cat)}}i  
stringval =~ catmatch

The Regexp::escape (ri it) will prevent your fears from coming true,  
here.

-austin
