Forum: Ruby Re: Beginner gsub and ri questions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jamal M. (Guest)
on 2006-04-17 20:38
(Received via mailing list)
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
Mark Van H. (Guest)
on 2006-04-17 21:37
(Received via mailing list)
I don't fully understand the situation when a regex would work. Could
you
give me one?

-mark
Austin Z. (Guest)
on 2006-04-17 21:46
(Received via mailing list)
On 4/17/06, Jamal M. <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
This topic is locked and can not be replied to.