Regexp Question / gsub Question

Discussion in 'Ruby' started by x1, Sep 26, 2006.

  1. x1

    x1 Guest

    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>"
     
    x1, Sep 26, 2006
    #1
    1. Advertising

  2. x1

    Mike Stok Guest

    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, Sep 26, 2006
    #2
    1. Advertising

  3. x1

    x1 Guest

    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.
    >
    >
    >
    >
    >
    >
     
    x1, Sep 26, 2006
    #3
  4. x1

    Mike Stok Guest

    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.
     
    Mike Stok, Sep 26, 2006
    #4
  5. x1

    x1 Guest

    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.
    >
    >
    >
    >
    >
    >
     
    x1, Sep 26, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kristof Bastiaensen

    Strange regexp behaviour in gsub

    Kristof Bastiaensen, May 12, 2004, in forum: Ruby
    Replies:
    14
    Views:
    199
    Simon Strandgaard
    May 15, 2004
  2. aurelianito

    gsub and gsub! are inconsistent

    aurelianito, Nov 8, 2005, in forum: Ruby
    Replies:
    9
    Views:
    165
    Robert Klemme
    Nov 9, 2005
  3. jackster the jackle

    Help with regexp and gsub

    jackster the jackle, Nov 28, 2008, in forum: Ruby
    Replies:
    2
    Views:
    93
    Robert Dober
    Nov 28, 2008
  4. Craig Jolicoeur
    Replies:
    9
    Views:
    149
    7stud --
    Apr 29, 2009
  5. Joao Silva
    Replies:
    16
    Views:
    368
    7stud --
    Aug 21, 2009
Loading...

Share This Page