K M Rakibul Islam
K M Rakibul Islam

Reputation: 34338

ruby regular expression to gsub a particular portion of a string

How to gsub a sub-string that starts with a particular character or enclosed by particular characters in ruby?

As for example :input is : Prime rates[ U.S. Effective Date: 12/16/2008 ] I want to gsub the sub-string that is enclosed by two square braces [ and ] and want output like : Prime Rates

How can I do that using ruby regular expression?

Upvotes: 1

Views: 132

Answers (1)

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70939

Maybe something like:

s.gsub(/\[.*\]/, "")

This will remove anything enclosed in [] assuming there is no more than one occurrence of such substring.

Upvotes: 3

Related Questions