Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regexp Question / gsub Question

Reply
Thread Tools

Regexp Question / gsub Question

 
 
x1
Guest
Posts: n/a
 
      09-26-2006
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>"

 
Reply With Quote
 
 
 
 
Mike Stok
Guest
Posts: n/a
 
      09-26-2006

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 <>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.





 
Reply With Quote
 
 
 
 
x1
Guest
Posts: n/a
 
      09-26-2006
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


On 9/25/06, Mike Stok <> wrote:
>
> 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 <>
> http://www.stok.ca/~mike/
>
> The "`Stok' disclaimers" apply.
>
>
>
>
>
>


 
Reply With Quote
 
Mike Stok
Guest
Posts: n/a
 
      09-26-2006

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

> 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 9/25/06, Mike Stok <> wrote:
>>
>> 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 <>
>> http://www.stok.ca/~mike/
>>
>> The "`Stok' disclaimers" apply.
>>
>>
>>
>>
>>
>>

>
>


--

Mike Stok <>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.





 
Reply With Quote
 
x1
Guest
Posts: n/a
 
      09-26-2006
Thanks so much Mike.

On 9/25/06, Mike Stok <> wrote:
>
> On 25-Sep-06, at 9:35 PM, x1 wrote:
>
> > 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 9/25/06, Mike Stok <> wrote:
> >>
> >> 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 <>
> >> http://www.stok.ca/~mike/
> >>
> >> The "`Stok' disclaimers" apply.
> >>
> >>
> >>
> >>
> >>
> >>

> >
> >

>
> --
>
> Mike Stok <>
> http://www.stok.ca/~mike/
>
> The "`Stok' disclaimers" apply.
>
>
>
>
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Issue with regexp pattern matcher withing String#gsub Craig Jolicoeur Ruby 9 04-29-2009 02:57 PM
Help with regexp and gsub jackster the jackle Ruby 2 11-28-2008 03:38 PM
gsub and gsub! are inconsistent aurelianito Ruby 9 11-09-2005 01:38 PM
Strange regexp behaviour in gsub Kristof Bastiaensen Ruby 14 05-15-2004 11:22 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57