例如:要将第一分组大写:
str = '1abc2aabbcc3aaabbbccc4'
pp str.sub(/(a{2})(b{2})/, '\1.upcase\2') # =>"1abc2aa.upcasebbcc3aaabbbccc4" 第一分组怎么没有变大写呢?
pp str.sub(/(a{2})(b{2})/, '\1'.upcase + '\2') # =>"1abc2aabbcc3aaabbbccc4" 奇怪,upcase()还是没起作用!?
pp str.sub(/(a{2})(b{2})/, $1.upcase + '\2') # =>"1abc2AAbbcc3aaabbbccc4" 使用$1而不是'\1',upcase()才起作用?
Two issues are involved here:
2, single quotation mark1,
'1'
和, 两种反向引用的区别
2, Ruby中单引号
'
和双引号"
and$1
, the difference between two back references'
and double quotation mark" in Ruby
The subtle difference'1'
而不是1
, 因为Ruby作为通用语言, 是不存在1
这种对象的. 换句话说, 你是不能单独写1
You may notice that I wrote'1'
用在匹配内,用在匹配外.
本质是全局变量, 只要发生正则匹配,
Ruby
就会更新这些全局变量. 此处主要是参考Perl
的正则设计.'1'
1
的确是正规的正则反向引用, 如在sed
中使用,echo str | sed -E 's,(a{2})(b{2}),21,g'
But in a broad sense,'1'
表示匹配的第一组, 即a
, 意思是重复两个a
Description: .'1'
But a special feature is that it can also be used when performing replacement'
和"
用法相同, 但在处理时有区别. 双引号的
"1"
是表示"u0001"
, 也就是说, 用来键入unicode
字符, 是一个字符. 而单引号的'1'
, 就是和
1
, 是两个字符, 如前述, 其本质是字符串, 当执行'1'.upcase
时, 是对两个字符执行大写, 但Ruby
中, 非小写字符的#upcase
In most cases,'
and"
have the same usage, but there are differences in processing. Double quotes
"1" means
"u0001"
, that is, it is used to type theunicode
character, which is one character. The single quoteand
1
are two characters. As mentioned above, they are essentially strings. When'1'.upcase
is executed, the two characters are uppercase, but InRuby
, the#upcase
method of non-lowercase characters returns itself.'1'
替换为aa
, 再执行#upcase
方法, 再替换原字串. 就我目前知道, 做不到这点, 除非ruby在语言层面提供新的语法, 否则,'1'
就只是一个字串, 它对匹配组的引用并不是变量存储的过程, 猜测可能是, 在底层执行的过程, 会自动将'..'
中的1
Considering what you mean here, you should first replaceaa
, then execute the#upcase
method, and then replace the original string. As far as I know, it can't be done In this regard, unless ruby provides new syntax at the language level, otherwise,is just a string, and its reference to the matching group is not a process stored in a variable. The guess is that the process executed at the bottom will automatically change
1
in >'..' with the quoted string.Answer:-
and - in single quotes are replaced with
- , and then connect with
, then capitalize 🎜🎜 🎜'1.upcase2'
, 单引号中的'1'
和'1'
分别被替换为aa
和bb
,.upcase
'1.upcase2'
,aa
andbb
respectively,.upcase unchanged.
'1'.upcase
为'1'
, 再与'2'
'1'.upcase
is'2'
.upcase
,变量存储字串
'aa'
try "\1"
When parameters are passed into a method, the parameters are calculated first and then the results are passed into the method