From: Michael Morin Date: 2008-08-26T18:32:37+09:00 Subject: Re: word boundaries for regular expressions Adam Akhtar wrote: > 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. > > am i doing something wrong here? If the spaces are consistent, you can do something like this "JP-ATH Headphones JP".split(/\s+/)[0..-2].join(" ") or this if they're not. "JP-ATH Headphones JP".sub(/\s+\w+$/,'') The advantage of the top one is you can remove something out of the middle of the string if necessary. The bottom one is probably faster and generally makes more sense. -- Michael Morin Guide to Ruby http://ruby.about.com/ Become an About.com Guide: beaguide.about.com About.com is part of the New York Times Company