# Regexp Question / gsub Question

**URL:** <https://www.ruby-forum.com/t/regexp-question-gsub-question/69980>\
**Category:** Ruby\
**Created:** [September 26, 2006, 3:28am UTC](https://www.ruby-forum.com/t/regexp-question-gsub-question/69980 "2006-09-26T03:28:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![d0t1q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@d0t1q](https://www.ruby-forum.com/u/d0t1q)\
**Post date:** [September 26, 2006, 3:28am UTC](https://www.ruby-forum.com/t/regexp-question-gsub-question/69980/1 "2006-09-26T03:28:19Z")

</div>

Perhaps someone could help with this example? I need to iterate  
through each of the items in strings and wrap any case insensitive  
occurrence of “key” with . If each string was always just the  
word “key” I would use something like wrapper.join(“key”) but… I have  
other text surrounding the “key”, various cases and potentially  
multiple occurrences. Thank you in advance.

wrapper = [" **", “** ”]  
strings = [“blahblahKEY”, “asdfasdfkey”, “Keyasdf”, “xxkEYxx”,  
“xxKeyxxKEY”]

strings.each do |string|  
# would like to print the string replacing “key” (case  
insensitive) with **“key”(in its original case)**  
end

# Ideally, would output:

# “blahblah **KEY** ”

# “asdfasdf **key** ”

# “ **Key** asdf”

# “xx **kEY** xx”

# “xx **Key** xx **KEY** ”

---

<div class="post-metadata">

**Author:** ![d0t1q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@d0t1q](https://www.ruby-forum.com/u/d0t1q)\
**Post date:** [September 26, 2006, 3:32am UTC](https://www.ruby-forum.com/t/regexp-question-gsub-question/69980/2 "2006-09-26T03:32:20Z")

</div>

On 25-Sep-06, at 9:27 PM, x1 wrote:

> “xxKeyxxKEY”]
> 
> # “ **Key** asdf”
> 
> # “xx **kEY** xx”
> 
> # “xx **Key** xx **KEY** ”

#!/usr/bin/env ruby

wrapper = [“ **”, “** ”]  
strings = [“blahblahKEY”, “asdfasdfkey”, “Keyasdf”, “xxkEYxx”,  
“xxKeyxxKEY”]

strings.each do |string|  
puts string.gsub(/key/i) {|match| “ **#{match}** ”}  
end

Maybe?

Hope this helps,

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![d0t1q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@d0t1q](https://www.ruby-forum.com/u/d0t1q)\
**Post date:** [September 26, 2006, 3:36am UTC](https://www.ruby-forum.com/t/regexp-question-gsub-question/69980/3 "2006-09-26T03:36:20Z")

</div>

Hehe… Awesome!!!

Ok… one last thing… : key being a variable.

How would you knock this one out? Instead of the string “key”, lets  
repace the occurance of the key variable, which in this case is “key”  
but could be another string…

key = “key”  
wrapper = [" **", “** ”]  
strings = [“blahblahKEY”, “asdfasdfkey”, “Keyasdf”, “xxkEYxx”,  
“xxKeyxxKEY”]

strings.each do |string|  
puts string.gsub(/key/i) {|match| “ **#{match}** ”}  
end

---

<div class="post-metadata">

**Author:** ![d0t1q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@d0t1q](https://www.ruby-forum.com/u/d0t1q)\
**Post date:** [September 26, 2006, 3:54am UTC](https://www.ruby-forum.com/t/regexp-question-gsub-question/69980/4 "2006-09-26T03:54:21Z")

</div>

Thanks so much Mike.

---

<div class="post-metadata">

**Author:** ![d0t1q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@d0t1q](https://www.ruby-forum.com/u/d0t1q)\
**Post date:** [September 26, 2006, 3:50am UTC](https://www.ruby-forum.com/t/regexp-question-gsub-question/69980/5 "2006-09-26T03:50:21Z")

</div>

On 25-Sep-06, at 9:35 PM, x1 wrote:

> strings = [“blahblahKEY”, “asdfasdfkey”, “Keyasdf”, “xxkEYxx”,  
> “xxKeyxxKEY”]
> 
> strings.each do |string|  
> puts string.gsub(/key/i) {|match| “ **#{match}** ”}  
> end

The simple, but incomplete/unsafe, approach might be:

thing = ‘key’  
strings.each do |string|  
puts string.gsub(/#{thing}/i) {|match| “ **#{match}** ”}  
end

If “thing” includes regex metacharacters then you might want to  
consider looking at Regexp::escape and figuring out how it could help  
those cases.

Hope this helps,

Mike

> > I have
> > 
> > > insensitive) with **“key”(in its original case)**
> > 
> > Maybe?  
> > The “`Stok’ disclaimers” apply.

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.
