正则表达式 - 正则分组匹配时,使用’\n ‘与 $n 为什么不同?
ringa_lee
ringa_lee 2017-04-24 15:59:42
0
3
969

例如:要将第一分组大写:
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()才起作用?

ringa_lee
ringa_lee

ringa_lee

reply all(3)
我想大声告诉你

Two issues are involved here:
1, '1', 两种反向引用的区别
2, Ruby中单引号'和双引号" and $1, the difference between two back references

2, single quotation mark ' and double quotation mark " in Ruby The subtle difference

'1'而不是1, 因为Ruby作为通用语言, 是不存在1这种对象的. 换句话说, 你是不能单独写1You may notice that I wrote

.

Question 1:

'1'用在匹配内, 用在匹配外. 本质是全局变量, 只要发生正则匹配, Ruby就会更新这些全局变量. 此处主要是参考Perl的正则设计. '1'

Essentially it is a string, but it is used as a back reference for the group when matching.

1的确是正规的正则反向引用, 如在sed中使用, echo str | sed -E 's,(a{2})(b{2}),21,g'But in a broad sense,

.

str =~ /(a{2})(b{2})/
  #=> 'aa'
str =~ /(a)(b)/
  #=> 'a'
str.sub(/(a{2})(b{2})/, '')  #=> 1abc2bbaacc3aaabbbccc4
'1'表示匹配的第一组, 即a, 意思是重复两个aDescription: .

'1'But a special feature is that it can also be used when performing replacement

.

Question 2:

'"用法相同, 但在处理时有区别. 双引号的"1"是表示"u0001", 也就是说, 用来键入unicode字符, 是一个字符. 而单引号的'1', 就是1, 是两个字符, 如前述, 其本质是字符串, 当执行'1'.upcase时, 是对两个字符执行大写, 但Ruby中, 非小写字符的#upcaseIn 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 the unicode character, which is one character. The single quote

is and 1 are two characters. As mentioned above, they are essentially strings. When '1'.upcase is executed, the two characters are uppercase, but In Ruby, the #upcase method of non-lowercase characters returns itself.

'1'替换为aa, 再执行#upcase方法, 再替换原字串. 就我目前知道, 做不到这点, 除非ruby在语言层面提供新的语法, 否则, '1'就只是一个字串, 它对匹配组的引用并不是变量存储的过程, 猜测可能是, 在底层执行的过程, 会自动将'..'中的1Considering what you mean here, you should first replace

with aa, 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:
  • '1.upcase2', 单引号中的'1''1'分别被替换为aabb, .upcase

    '1.upcase2',
  • and
  • in single quotes are replaced with aa and bb respectively, .upcase unchanged.

    '1'.upcase'1', 再与'2'

  • '1'.upcase is
  • , and then connect with '2'

    .upcase, 变量存储字串'aa'

, then capitalize 🎜🎜 🎜
左手右手慢动作

try "\1"

Peter_Zhu

When parameters are passed into a method, the parameters are calculated first and then the results are passed into the method

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!