Robert Klemme
8/26/2008 4:23:00 PM
2008/8/26 Adam Akhtar <adamtemporary@gmail.com>:
> Hi did a search for word boundaries but didnt quite find what i was
> looking for.
>
> If i have strings containing products and model numbers
>
> e.g.
> "JP-ATH Headphones JP"
>
> and I want to remove the last JP but not the one in the modle number how
> do i go about it,
>
> i tried
>
> string.gsub(/\bJP\b/, '')
> but it removes both.
> I guess the hypen in the model number doesnt count as a word letter so
> it gets knocked off.
Yep, that sums it up pretty well.
> am i doing something wrong here?
Obviously, since your results do not match your expectations / requirements. :-)
You could use lookahead
irb(main):002:0> "JP-ATH Headphones JP".gsub /\bJP\b(?=\s|$)/, 'XXX'
=> "JP-ATH Headphones XXX"
It all depends on what other occurrences you have and which of them
you want to match.
Kind regards
robert
--
use.inject do |as, often| as.you_can - without end