Dave Dave - 1 year ago 73
Ruby Question

Why is "\b" not catching my word boundary in Rails?

I’m using Rails 4.2.7. I want to match an expression and take advantage of the word boundary expression because there may be a space, arbitrary number of spaces, or a dash between my words. But this isn’t working

2.3.0 :006 > /first\bsecond/.match('first second')
=> nil

The manual here — https://ruby-doc.org/core-2.1.1/Regexp.html suggests that “\b” is the right expression to use to catch word boundaries so I’m wondering where I’m going wrong.

Sam Sam
Answer Source

\b matches a zero-length word boundary, not a space. You're looking for something more like this:

/first\b.\bsecond/.match('first second')

This will match any character (.) in between first and second, as long as there is a word boundary on either side.

However, this is not how word boundaries are usually used (since there is no need to use a zero-length check when you are matching the word boundary itself). \b is essentially looking for a non-word character after a word character; so, instead, you could just look for a non-word character in-between the t in first and s in second:

/first\Wsecond/.match('first second')

This is exactly the same as the first example...but, realistically, you probably just want to match whitespace and can use something like this:

/first\ssecond/.match('first second')

@WiktorStribiżew's third example shows the best use of word boundaries (at the beginning and end). This is because you aren't matching anything before or after, so a zero-length test is helpful. Otherwise, the above examples could match something like first secondary. In the end, I'd use an expression like:

/\bfirst\ssecond\b/.match('first second')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download