gpt4 book ai didi

ruby 正则表达式不一致

转载 作者:太空宇宙 更新时间:2023-11-03 18:08:26 24 4
gpt4 key购买 nike

我正在尝试使用带反向引用的 gsub 将其转换为罗马数字,但我发现了一个奇怪的不一致之处。

$ ruby -v 
ruby 2.2.5p319 (2016-04-26 revision 54774) [x86_64-linux]

$ irb
irb(main):001:0> BASES = {
irb(main):002:1* 1000 => 'M',
irb(main):003:1* 500 => 'D',
irb(main):004:1* 100 => 'C',
irb(main):005:1* 50 => 'L',
irb(main):006:1* 10 => 'X',
irb(main):007:1* 5 => 'V',
irb(main):008:1* 1 => 'I'
irb(main):009:1> }
=> {1000=>"M", 500=>"D", 100=>"C", 50=>"L", 10=>"X", 5=>"V", 1=>"I"}
irb(main):010:0> BASE_KEYS = BASES.keys
=> [1000, 500, 100, 50, 10, 5, 1]
irb(main):011:0> rom = 'IIII'
=> "IIII"

上面是设置

下面我试图识别任何重复 4 次的字符,并将其替换为该字符的 1 个和下一个 BASE 字符。

例如:III => IV

irb(main):012:0>     rom.gsub(/((.)\2{3})/,
irb(main):013:1* "#{
irb(main):014:0> BASES[BASE_KEYS.select.with_index{ |bk, i|
irb(main):015:2> BASES[BASE_KEYS[i]] == $2
irb(main):016:2> }.first]
irb(main):017:0> }
irb(main):018:1" #{BASE_KEYS.select.with_index{ |bk, i|
irb(main):019:1> BASES[BASE_KEYS[i]] == $2
irb(main):020:1> }.first}
irb(main):021:1" #{
irb(main):022:0> BASES[BASE_KEYS.select.with_index{|bk, i|
irb(main):023:2> BASES[BASE_KEYS[i+1]] == $2
irb(main):024:2> }.first]
irb(main):025:0> }
irb(main):026:1" #{BASE_KEYS.select.with_index{ |bk, i|
irb(main):027:1> BASES[BASE_KEYS[i+1]] == $2
irb(main):028:1> }.first}
irb(main):029:1" "
irb(main):030:1> )
=> "\n \n I\n 1\n "

所以我得到了错误的答案..(带有调试信息以获得更多见解)

irb(main):031:0>     rom.gsub(/((.)\2{3})/,
irb(main):032:1* "#{
irb(main):033:0> BASES[BASE_KEYS.select.with_index{ |bk, i|
irb(main):034:2> BASES[BASE_KEYS[i]] == $2
irb(main):035:2> }.first]
irb(main):036:0> }
irb(main):037:1" #{BASE_KEYS.select.with_index{ |bk, i|
irb(main):038:1> BASES[BASE_KEYS[i]] == $2
irb(main):039:1> }.first}
irb(main):040:1" #{
irb(main):041:0> BASES[BASE_KEYS.select.with_index{|bk, i|
irb(main):042:2> BASES[BASE_KEYS[i+1]] == $2
irb(main):043:2> }.first]
irb(main):044:0> }
irb(main):045:1" #{BASE_KEYS.select.with_index{ |bk, i|
irb(main):046:1> BASES[BASE_KEYS[i+1]] == $2
irb(main):047:1> }.first}
irb(main):048:1" "
irb(main):049:1> )
=> "I\n 1\n V\n 5\n "
irb(main):050:0>

诚然,我的正则表达式代码很难理解,但为什么我在第二次调用相同代码时得到不同的结果?

irb(main):050:0> rom
=> "IIII"

注意 rom 没有改变...

最佳答案

您的代码在计算正则表达式之前使用了 $2。第一次运行它后 $2 被设置并且代码按预期工作。考虑使用 block 而不是字符串,因为您的字符串是在匹配发生之前插入的。

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. The value returned by the block will be substituted for the match on each call.

这是一致的:

rom.gsub(/((.)\2{3})/) { |s|
"#{
BASES[BASE_KEYS.select.with_index{ |bk, i|
BASES[BASE_KEYS[i]] == $2
}.first]
}
#{BASE_KEYS.select.with_index{ |bk, i|
BASES[BASE_KEYS[i]] == $2
}.first}
#{
BASES[BASE_KEYS.select.with_index{|bk, i|
BASES[BASE_KEYS[i+1]] == $2
}.first]
}
#{BASE_KEYS.select.with_index{ |bk, i|
BASES[BASE_KEYS[i+1]] == $2
}.first}
"
}
# => "I\n 1\n V\n 5\n "

关于 ruby 正则表达式不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39313087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com