[英]Replace keys of a hash with its value inside a specific group of a matched regex
我有一个输入:
input = gets
其中一个子串的形式:
"本資料由(Name of a contractor)提供"
出现在不同的位置。 我还有数以千计的承包商名称及其英文音译存储在一个哈希中:
hash = {'key1': 'value1', 'key2': 'value2'}
我有以下代码:
input.gsub!(/本資料由(.+)提供/) {"\nThe following information has been provided by: #{$1}\n"}
# => The following information has been provided by: (name of contractor)
要将本地名称更改为英文名称,我可以执行以下操作:
person_making_announcement = /(The following information has been provided by: )(.+)/.match(input)
if Company_making_the_Announcement[2].match "key1"
input.gsub! Company_making_the_Announcement[2], "value1"
elsif Company_making_the_Announcement[2].match "key2"
input.gsub! Company_making_the_Announcement[2], "value2"
end
但这非常笨拙,无论如何我都需要将它们放在散列中以用于代码的其他部分。 如果我做:
hash.each do |k, v|
input.gsub!("#{k}", "#{v}")
end
然后input
中的所有匹配项都会更改。 如果我改变方法使用sub!
,只会更改第一个实例。 我认为以下方法可行:
myregex = /(There is text here: )(.+)/.match(input)
hash.each do |k, v|
myregex[2].gsub!("#{k}", "#{v}")
end
但事实并非如此。 我需要保留正则表达式,因为它是替换的一部分,并且之前对输入进行了修改。
仅在与输入匹配的正则表达式中的特定子组内进行更改的语法是什么?
我想你可能会关注以下内容,但如果我错了,请告诉我。
def replace_em(str, h)
str.gsub(/\S+/, Hash.new { |_,k| k }.merge(h))
end
h = { 'k1'=>'v1', 'k2'=>'v2' }
str = "Now is the k1 for all good persons to k2 to their friend k11"
replace_em(str, h)
#=> "Now is the v1 for all good persons to v2 to their friend k11"
/\\S+/
匹配不包含空格的字符串。
g = Hash.new { |_,k| k }
创建具有一种使得默认PROC一个空散列g[k]
以返回k
如果g
不具有关键k
。
g.merge(h)
返回一个散列,该散列具有刚刚使用h
键和值创建的默认 proc。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.