Regexp Question / gsub Question

X

x1

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 <b> </b>. 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 = ["<b>", "</b>"]
strings = ["blahblahKEY", "asdfasdfkey", "Keyasdf", "xxkEYxx", "xxKeyxxKEY"]

strings.each do |string|
# would like to print the string replacing "key" (case
insensitive) with <b>"key"(in its original case)</b>
end


# Ideally, would output:
# "blahblah<b>KEY</b>"
# "asdfasdf<b>key</b>"
# "<b>Key</b>asdf"
# "xx<b>kEY</b>xx"
# "xx<b>Key</b>xx<b>KEY</b>"
 
M

Mike Stok

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 <b> </b>. 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 = ["<b>", "</b>"]
strings = ["blahblahKEY", "asdfasdfkey", "Keyasdf", "xxkEYxx",
"xxKeyxxKEY"]

strings.each do |string|
# would like to print the string replacing "key" (case
insensitive) with <b>"key"(in its original case)</b>
end


# Ideally, would output:
# "blahblah<b>KEY</b>"
# "asdfasdf<b>key</b>"
# "<b>Key</b>asdf"
# "xx<b>kEY</b>xx"
# "xx<b>Key</b>xx<b>KEY</b>"

#!/usr/bin/env ruby

wrapper = ["<b>", "</b>"]
strings = ["blahblahKEY", "asdfasdfkey", "Keyasdf", "xxkEYxx",
"xxKeyxxKEY"]

strings.each do |string|
puts string.gsub(/key/i) {|match| "<b>#{match}</b>"}
end

Maybe?

Hope this helps,

Mike

--

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
X

x1

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 = ["<b>", "</b>"]
strings = ["blahblahKEY", "asdfasdfkey", "Keyasdf", "xxkEYxx",
"xxKeyxxKEY"]

strings.each do |string|
puts string.gsub(/key/i) {|match| "<b>#{match}</b>"}
end


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 <b> </b>. 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 = ["<b>", "</b>"]
strings = ["blahblahKEY", "asdfasdfkey", "Keyasdf", "xxkEYxx",
"xxKeyxxKEY"]

strings.each do |string|
# would like to print the string replacing "key" (case
insensitive) with <b>"key"(in its original case)</b>
end


# Ideally, would output:
# "blahblah<b>KEY</b>"
# "asdfasdf<b>key</b>"
# "<b>Key</b>asdf"
# "xx<b>kEY</b>xx"
# "xx<b>Key</b>xx<b>KEY</b>"

#!/usr/bin/env ruby

wrapper = ["<b>", "</b>"]
strings = ["blahblahKEY", "asdfasdfkey", "Keyasdf", "xxkEYxx",
"xxKeyxxKEY"]

strings.each do |string|
puts string.gsub(/key/i) {|match| "<b>#{match}</b>"}
end

Maybe?

Hope this helps,

Mike

--

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
M

Mike Stok

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 = ["<b>", "</b>"]
strings = ["blahblahKEY", "asdfasdfkey", "Keyasdf", "xxkEYxx",
"xxKeyxxKEY"]

strings.each do |string|
puts string.gsub(/key/i) {|match| "<b>#{match}</b>"}
end

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

thing = 'key'
strings.each do |string|
puts string.gsub(/#{thing}/i) {|match| "<b>#{match}</b>"}
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
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 <b> </b>. 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 = ["<b>", "</b>"]
strings = ["blahblahKEY", "asdfasdfkey", "Keyasdf", "xxkEYxx",
"xxKeyxxKEY"]

strings.each do |string|
# would like to print the string replacing "key" (case
insensitive) with <b>"key"(in its original case)</b>
end


# Ideally, would output:
# "blahblah<b>KEY</b>"
# "asdfasdf<b>key</b>"
# "<b>Key</b>asdf"
# "xx<b>kEY</b>xx"
# "xx<b>Key</b>xx<b>KEY</b>"

#!/usr/bin/env ruby

wrapper = ["<b>", "</b>"]
strings = ["blahblahKEY", "asdfasdfkey", "Keyasdf", "xxkEYxx",
"xxKeyxxKEY"]

strings.each do |string|
puts string.gsub(/key/i) {|match| "<b>#{match}</b>"}
end

Maybe?

Hope this helps,

Mike

--

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

--

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
X

x1

Thanks so much Mike.

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 = ["<b>", "</b>"]
strings = ["blahblahKEY", "asdfasdfkey", "Keyasdf", "xxkEYxx",
"xxKeyxxKEY"]

strings.each do |string|
puts string.gsub(/key/i) {|match| "<b>#{match}</b>"}
end

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

thing = 'key'
strings.each do |string|
puts string.gsub(/#{thing}/i) {|match| "<b>#{match}</b>"}
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
On 25-Sep-06, at 9:27 PM, x1 wrote:

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 <b> </b>. 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 = ["<b>", "</b>"]
strings = ["blahblahKEY", "asdfasdfkey", "Keyasdf", "xxkEYxx",
"xxKeyxxKEY"]

strings.each do |string|
# would like to print the string replacing "key" (case
insensitive) with <b>"key"(in its original case)</b>
end


# Ideally, would output:
# "blahblah<b>KEY</b>"
# "asdfasdf<b>key</b>"
# "<b>Key</b>asdf"
# "xx<b>kEY</b>xx"
# "xx<b>Key</b>xx<b>KEY</b>"


#!/usr/bin/env ruby

wrapper = ["<b>", "</b>"]
strings = ["blahblahKEY", "asdfasdfkey", "Keyasdf", "xxkEYxx",
"xxKeyxxKEY"]

strings.each do |string|
puts string.gsub(/key/i) {|match| "<b>#{match}</b>"}
end

Maybe?

Hope this helps,

Mike

--

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

--

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top