Why do I need \b in regular expression?

Hello once again folks!

OK, I’m still extremely new to Ruby, so if my question is stupid, please
be gentle :).

The code below is an example from a ruby book, and I look at it, I
understand most of it, but what I don’t understand is why the
author/ruby-expert has \b in the regular expression (I know it’s a
backspace), but why match for a backspace especially it’s outside of the
bracket, and why the brackets? In this context, I’m confused! I’m
talking about line number 3! Any clarification or insight would be
really helpful to me. Thank you!

class WordPlay
def self.switch_pronouns(text)
text.gsub(/\b(I am|You are|I|You|Me|Your|My)\b/i) do |pronoun|
case pronoun.downcase
when “i”
“you”
when “you”
“me”
when “me”
“you”
when “i am”
“you are”
when “you are”
“i am”
when “your”
“my”
when “my”
“your”
end
end.sub(/^me\b/i, ‘i’)
end

Think of \b as the boundary of the words in a string.

\B has the opposite effect, affecting all letter/character boundaries
instead of whole words.

string = “foo bar”
if you gsub this using \b, it will look like this:

string.gsub(/\b/,"-") #=> -foo- -bar-

As you can tell, it has added a “-” to only the boundaries (edges) of
each word.

using \B will output this #=> f-o-o b-a-r

These are the inside boundaries of each word instead of the outside
boundaries.
I hope this clarifies things a little.

Regards,

  • Mac

On 15.03.2009 18:57, Power O. wrote:

OK, I’m still extremely new to Ruby, so if my question is stupid, please
be gentle :).

It’s not stupid at all. Regular expressions have a bit of a learning
curve, especially if one lacks the formal theory of computer languages.

The code below is an example from a ruby book, and I look at it, I
understand most of it, but what I don’t understand is why the
author/ruby-expert has \b in the regular expression (I know it’s a
backspace),

No, it’s not in this context. As Michael pointed out, it’s a word
boundary anchor, one of several zero width assertions that you can have
in your regular expression. These assertions do not contain characters
when matching but they only match in certain places, in the case of \b
at a word boundary (i.e. the character before is a word character and
the one after a non word character or the other way round).

but why match for a backspace especially it’s outside of the
bracket, and why the brackets? In this context, I’m confused! I’m
talking about line number 3! Any clarification or insight would be
really helpful to me. Thank you!

class WordPlay
def self.switch_pronouns(text)
text.gsub(/\b(I am|You are|I|You|Me|Your|My)\b/i) do |pronoun|

The capturing group in this case is not really needed but you need
grouping because of precedence rules and the presence of “|” inside the
expression. For the non capturing version you can do

/\b(?:I am|You are|I|You|Me|Your|My)\b/i

Kind regards

robert

Thank you guy! You guys are so helpful. I also did a search on google
and saw that you guys hit the nail right on its head. Now I understand
what \b is! It seems that if you want to search for a whole phrase,
sentence, or a word, you need to use two \b (that’s \bwhatever here\b).