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')
\b matches a zero-length word boundary, not a space. You're looking for something more like this:
This will match any character (
.) in between
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
This is exactly the same as the first example...but, realistically, you probably just want to match whitespace and can use something like this:
@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: