with my regular expression or substitution string?
To find out how your strings are being parsed, print them out. Then
print
out the result of the regexes directly, rather than relying on an
assertion.
I’ve done that already, the test was only to show the problem: I could
not escape chars in the numbers string with a backslash.
Anyway, I found the solution, it’s five backslashes instead of four.
That’s a bit counter-intuitive, maybe someone can explain it.
Especially when these two are compared:
I expected that when I remove the space from the first expression, that
my characters would get quoted. instead, the four backslashes get
interpreted as two escaped backslashes and the 1 as a literal
character. Can somebdoy shed some light on the how and why of this
case? Especially, why the solution with the five backslashes doesn’t
yield double backlashes in the result string?
I expected that when I remove the space from the first expression, that
my characters would get quoted. instead, the four backslashes get
interpreted as two escaped backslashes and the 1 as a literal
character. Can somebdoy shed some light on the how and why of this
case? Especially, why the solution with the five backslashes doesn’t
yield double backlashes in the result string?
In the replacement string, a backreference is a backslash followed by a
number – reference(\1) – but a double-backslash is treated as a
literal single backslash, so \1 == literal(\1). So then, three
backslashes and a number, \\1 is equal to literal() reference(\1).
Four means literal(\1). Finally, five means literal(\) reference(\1),
and thus, since backslashes must be escaped to be seen as a single
backslash in a string, you end up with the resulting string
“1\23\45”, meaning 1\23\45. Hope that makes sense.
To find out how your strings are being parsed, print them out.
/ …
I expected that when I remove the space from the first expression, that
my characters would get quoted. instead, the four backslashes get
interpreted as two escaped backslashes and the 1 as a literal
character. Can somebdoy shed some light on the how and why of this
case?
Sure. Parsing these strings is a trivial exercise. Each adjacent pair of
backslashes collapses into one literal backslash, and any orphan
backslashes are associated with the character to its immediate right.
Especially, why the solution with the five backslashes doesn’t
yield double backlashes in the result string?
To sort out how Ruby is parsing your strings, printthemout.
puts ‘\\1’
\1 # meaning: a backslash and an escaped ‘1’
puts ‘\ \1’
\ \1 # meaning a backslash, a space, and an escaped ‘1’
puts ‘\\\1’
\\1 # meaning two backslashes and an escaped ‘1’
Oh, by the way. You haven’t said what you are trying to accomplish.
Oh, by the way. You haven’t said what you are trying to accomplish.
I was trying to escape some characters in a string with a backslash.
When printing out ‘\\\1’ (resulting in two backslashes and and
escaped ‘1’ like you said) I would expect the result string s
(s=numbers.gsub(/(2|4)/, ‘\\\1’) to contain two backslashes and
then the original character. But apparently the replacement string is
interpreted as “one backslash and a backreference (escaped with two
backslashes).”
But apparently the replacement string is
interpreted as “one backslash and a backreference (escaped with two
backslashes).”
After thinking a while about it I realized this is not correct.
Backslashes in a replacement string must be double backslashes (four
backslashes in the literal string) because otherwise they would be
interpreted as escaped characters by the regex engine. Right?
After thinking a while about it I realized this is not correct.
Backslashes in a replacement string must be double backslashes (four
backslashes in the literal string) because otherwise they would be
interpreted as escaped characters by the regex engine. Right?
Yes, if the backslashes are followed by anything other than another
backslash (a backslash used as an escape must be followed by a target
character other than another backslash). This is why it’s a good idea to
print the string you intend to use. Printing the string forces it to be
parsed, so you can see what you are getting into.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.