Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: unexplained behaviour of String#sub in the presence of "\\'"

Tags:

ruby

I can't understand why this happens:

irb(main):015:0> s = "Hello\\'World"
=> "Hello\\'World"
irb(main):016:0> "#X#".sub("X",s)
=> "#Hello#World#"

I would have thought the output would be "#Hello\'World#", and I certainly can't understand where the extra # came from.

I guess I'm unfamiliar with something that has got to do with the internals of String#sub and to the "\'" symbols.

like image 887
Gadi A Avatar asked Jan 25 '26 16:01

Gadi A


1 Answers

It's due to the use of backslash in a sub replacement string.

Your replacement string contains \' which is expanded to the global variable $' which is otherwise known as POSTMATCH. For a string replacement, it contains everything in the string which exists following the matched text. So because your X that you replaced is followed by a #, that's what gets inserted.

Compare:

"#X$".sub("X",s)
=> "#Hello$World$"

Note that the documentation for sub refers to use of backreferences \0 through \9. This seems to refer directly to the global variables $0 to $9 and also applies to other global variables.

For reference, the other global variables set by regular expression matching are:

$~ is equivalent to ::last_match;

$& contains the complete matched text;

$` contains string before match;

$' contains string after match;

$1, $2 and so on contain text matching first, second, etc capture group;

$+ contains last capture group.
like image 125
Gareth Avatar answered Jan 28 '26 22:01

Gareth