What does \ b really mean in Ruby regular expressions?

I have a file with phrases such as "Canyon St / 27th Way" that I am trying to turn into "Canyon St and 27th Way" with Ruby regular expressions.

I used file = file.gsub(/(\b) \/ (\b)/, "#{$1} and #{$2}") to make a match, but I'm a little puzzled by what \ b really means and why $ 1 contains all characters before the word boundary preceding the slash, and why $ 2 contains all characters after the word boundary begins with the next word.

I usually expect that everything in parentheses in the regular expression will be in $ 1 and $ 2, but I'm not sure that the parentheses around the word border really mean, because there is a space between the transition from the word character to the space character.

+5
source share
2 answers

$ 1 and $ 2 are not really related to your regular expression: method arguments are evaluated before the method is called, therefore

 "#{$1} and #{$2}" 

Evaluated before the regular expression matches your string. If you did not execute earlier regular expressions, then these variables will be equal to zero, therefore you actually execute

 file = file.gsub(/(\b) \/ (\b)/, " and ") 

that is, you replace the forward slash surrounded by spaces, the "and", also surrounded by spaces. $ 1 and $ 2 will be updated to be empty lines, and therefore you will see the same behavior when processing the next line.

+6
source

In this context, parentheses do nothing. You can get the same result using /\b \/ \b/ .

I think you are a little confused by $1 and $2 . In fact, they are not doing anything. They are zero because they do not match anything (just a word of boundary). What you wrote is the logical equivalent of .gsub(/\b \/ \b/, " and ")

+8
source

All Articles