[英]Inconsistent behavior in Ruby gsub replacement?
The two gsub's yield different outcomes. 两个gsub产生不同的结果。 Can anybody explain why?
谁能解释为什么?
Code is also available at https://gist.github.com/franklsf95/6c0f8938f28706b5644d . 代码也可以在https://gist.github.com/franklsf95/6c0f8938f28706b5644d获得 。
ver = 9999
str = "\t<key>CFBundleDevelopmentRegion</key>\n\t<string>en</string>\n\t<key>CFBundleVersion</key>\n\t<string>0.1.190</string>\n\t<key>AppID</key>\n\t<string>000000000000000</string>"
puts str.gsub /(CFBundleVersion<\/key>\n\t.*\.).*(<\/string>)/, "#{$1}#{ver}#{$2}"
puts '--------'
puts str.gsub /(CFBundleVersion<\/key>\n\t.*\.).*(<\/string>)/, "#{$1}#{ver}#{$2}"
My ruby version is ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
(MRI). 我的ruby版本是
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
(MRI)。 On my machine, the outcome is: 在我的机器上,结果是:
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>9999
<key>AppID</key>
<string>000000000000000</string>
--------
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleVersion</key>
<string>0.1.9999</string>
<key>AppID</key>
<string>000000000000000</string>
The second one is the desired effect, but the first one is wrong. 第二个是期望的效果,但第一个是错误的。
It has to do with timing and how ruby regexes work. 它与时间和红宝石正则表达式的工作方式有关。
gsub
sets $1
and $2
, but not until after it completes. gsub
设置$1
和$2
,但直到完成后才设置。 So when you run the first time through, they're blank. 因此,当您第一次运行时,它们是空白的。 When you run the second time, they were set by the previous
gsub
. 当你第二次运行时,它们是由前一个
gsub
设置的。 If you want to do regex captures in place, you need \\1
and \\2
, like this: 如果你想在适当的位置进行正则表达式捕获,你需要
\\1
和\\2
,如下所示:
puts str.gsub /(CFBundleVersion<\/key>\n\t.*\.).*(<\/string>)/, '\1' + ver.to_s + '\2'
If you use the block form of gsub(), your code will work correctly: 如果你使用gsub()的块形式,你的代码将正常工作:
ver = 9999
str = "\t<key>CFBundleDevelopmentRegion</key>\n\t<string>en</string>\n\t<key>CFBundleVersion</key>\n\t<string>0.1.190</string>\n\t<key>AppID</key>\n\t<string>000000000000000</string>"
puts str.gsub(/(CFBundleVersion<\/key>\n\t.*\.).*(<\/string>)/) {|match|
"#{$1}#{ver}#{$2}"
}
puts '-' * 20
puts str.gsub(/(CFBundleVersion<\/key>\n\t.*\.).*(<\/string>)/) {|match|
"#{$1}#{ver}#{$2}"
}
--output:--
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleVersion</key>
<string>0.1.9999</string>
<key>AppID</key>
<string>000000000000000</string>
--------------------
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleVersion</key>
<string>0.1.9999</string>
<key>AppID</key>
<string>000000000000000</string>
The docs describe this behavior: 文档描述了这种行为:
If replacement is a String, ... However, within replacement the special match variables, such as $&, will not refer to the current match.
如果replacement是String,...但是,在替换中,特殊匹配变量(例如$&)将不会引用当前匹配。
...
...
In the block form, the current match string is passed in as a parameter, and variables such as $1, $2, $`, $&, and $' will be set appropriately.
在块形式中,当前匹配字符串作为参数传入,并且将适当地设置诸如$ 1,$ 2,$`,$&和$'的变量。 The value returned by the block will be substituted for the match on each call.
块返回的值将替换每次调用的匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.