Forum: Ruby Why do I need \b in regular expression?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
14e8bd391ddbb9a311edac1d774cd38a?d=identicon&s=25 Power One (arghx1)
on 2009-03-15 19:00
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
57a778becb5ea951889fa9f0b4e3bd15?d=identicon&s=25 Michael Linfield (macgoober)
on 2009-03-15 19:24
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-03-15 20:10
(Received via mailing list)
On 15.03.2009 18:57, Power One 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
14e8bd391ddbb9a311edac1d774cd38a?d=identicon&s=25 Power One (arghx1)
on 2009-03-16 01:00
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).
This topic is locked and can not be replied to.