# Word boundaries for regular expressions

**URL:** <https://www.ruby-forum.com/t/word-boundaries-for-regular-expressions/146862>\
**Category:** Ruby\
**Created:** [August 26, 2008, 9:37am UTC](https://www.ruby-forum.com/t/word-boundaries-for-regular-expressions/146862 "2008-08-26T09:37:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 26, 2008, 9:37am UTC](https://www.ruby-forum.com/t/word-boundaries-for-regular-expressions/146862/1 "2008-08-26T09:37:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 26, 2008, 9:45am UTC](https://www.ruby-forum.com/t/word-boundaries-for-regular-expressions/146862/2 "2008-08-26T09:45:24Z")

</div>

On Tuesday 26 August 2008, Adam A. wrote:

> 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?

You can replace the first \b with \s, which only matches spaces:

string.gsub(/\sJP\b/, ‘’)

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 26, 2008, 11:37am UTC](https://www.ruby-forum.com/t/word-boundaries-for-regular-expressions/146862/3 "2008-08-26T11:37:36Z")

</div>

Adam A. wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 26, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/word-boundaries-for-regular-expressions/146862/4 "2008-08-26T18:27:58Z")

</div>

2008/8/26 Adam A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [August 28, 2008, 7:11am UTC](https://www.ruby-forum.com/t/word-boundaries-for-regular-expressions/146862/5 "2008-08-28T07:11:48Z")

</div>

Thanks everyone. Yes the strings to match vary a lot in terms of  
positiong. Some dont have model numbers, some do, some dont have jp some  
do. Ive know of look ahead but never used it before. ill give that a  
shot.

Thanks!

adam
