在字符串字面值中作为`String#tr`的参数的转义状态 [英] Escape status within a string literal as argument of `String#tr`

查看:183
本文介绍了在字符串字面值中作为`String#tr`的参数的转义状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于单引号字符串中的反斜杠的转义状态,作为 String#tr 的参数,有一些神秘的东西。你能解释下面三个例子的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用'd',这并不会改变双引号中转义的含义(\d = d)。

 '\\ \\\'.tr('\\','x')#=> x
'\\'.tr('\\d','x')#=> \\
'\\'.tr('\\\\d','x')#=> x


解决方案

tr



tr 的第一个参数非常像正则表达式中的括号字符分组。您可以在表达式的开头使用 ^ 来否定匹配(替换不匹配的任何内容),并使用例如 a-f 以匹配一系列字符。由于它具有控制字符,它也在内部进行转义,因此您可以使用 - ^ 作为文字字符。 / p>

  print'abcdef'.tr('b-e','x')#axxxxf 
print'abcdef' .tr('b \ -e','x')#axcdxf



引用字符串



此外,当使用单引号时,Ruby尽可能包含反斜杠,即当它不用于实际转义另一个反斜杠或单引号时。 p>

 #单引号
打印'\\'#\
打印'\d'# \d
打印'\\d'#\d
打印'\\\d'#\\d

#双引号
打印\\#\
打印\d#d
打印\\d#\d
打印\ \\d#\d



重新访问的示例

有了这一切,让我们再来看看这些例子。

  '\\'.tr('\\','x')#=> x

定义为'\\'成为文字字符串 \ ,因为第一个反斜杠转义为第二个。

 '\\'.tr('\\d','x')# => \\

定义为'\\ d'成为文字字符串 \d tr 引擎,反过来使用文字字符串中的反斜杠来转义 d 。结果: tr 用x替换 d 的实例。

 '\\'.tr('\\\\d','x')#=> x

定义为'\\\\d '成为文字 \\d 。首先 \\ 成为 \ 。然后 \d 变为 \d ,即反斜杠保留。 (这个特殊的行为不同于双重字符串,反斜杠将会活着,只留下一个寂寞的 d



文字字符串 \\d 然后使 tr 替换所有字符,它们是反斜杠或 d 替换字符串。


There is something mysterious to me about the escape status of a backslash within a single quoted string literal as argument of String#tr. Can you explain the contrast between the three examples below? I particularly do not understand the second one. To avoid complication, I am using 'd' here, which does not change the meaning when escaped in double quotation ("\d" = "d").

'\\'.tr('\\', 'x')      #=> "x"
'\\'.tr('\\d', 'x')     #=> "\\"
'\\'.tr('\\\d', 'x')    #=> "x"

解决方案

Escaping in tr

The first argument of tr works much like bracket character grouping in regular expressions. You can use ^ in the start of the expression to negate the matching (replace anything that doesn't match) and use e.g. a-f to match a range of characters. Since it has control characters, it also does escaping internally, so you can use - and ^ as literal characters.

print 'abcdef'.tr('b-e', 'x')  # axxxxf
print 'abcdef'.tr('b\-e', 'x') # axcdxf

Escaping in Ruby single quote strings

Furthermore, when using single quotes, Ruby tries to include the backslash when possible, i.e. when it's not used to actually escape another backslash or a single quote.

# Single quotes
print '\\'    # \
print '\d'    # \d
print '\\d'   # \d
print '\\\d'  # \\d

# Double quotes
print "\\"    # \
print "\d"    # d
print "\\d"   # \d
print "\\\d"  # \d

The examples revisited

With all that in mind, let's look at the examples again.

'\\'.tr('\\', 'x')      #=> "x"

The string defined as '\\' becomes the literal string \ because the first backslash escapes the second. No surprises there.

'\\'.tr('\\d', 'x')     #=> "\\"

The string defined as '\\d' becomes the literal string \d. The tr engine, in turn uses the backslash in the literal string to escape the d. Result: tr replaces instances of d with x.

'\\'.tr('\\\d', 'x')    #=> "x"

The string defined as '\\\d' becomes the literal \\d. First \\ becomes \. Then \d becomes \d, i.e. the backslash is preserved. (This particular behavior is different from double strings, where the backslash would be eaten alive, leaving only a lonesome d)

The literal string \\d then makes tr replace all characters that are either a backslash or a d with the replacement string.

这篇关于在字符串字面值中作为`String#tr`的参数的转义状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆