[英]Replace keys of a hash with its value inside a specific group of a matched regex
I have an input:我有一个输入:
input = gets
in which a substring of the form:其中一个子串的形式:
"本資料由(Name of a contractor)提供"
appears at different positions.出现在不同的位置。 I also have thousands of contractor names and their English transliterations stored in a hash:我还有数以千计的承包商名称及其英文音译存储在一个哈希中:
hash = {'key1': 'value1', 'key2': 'value2'}
I have the following code:我有以下代码:
input.gsub!(/本資料由(.+)提供/) {"\nThe following information has been provided by: #{$1}\n"}
# => The following information has been provided by: (name of contractor)
To change a native name into English name, I can do:要将本地名称更改为英文名称,我可以执行以下操作:
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
But this is very clumsy, and I need them in a hash anyways for other parts of the code.但这非常笨拙,无论如何我都需要将它们放在散列中以用于代码的其他部分。 If I do:如果我做:
hash.each do |k, v|
input.gsub!("#{k}", "#{v}")
end
then all matches in input
are changed.然后input
中的所有匹配项都会更改。 If I change the method to use sub!
如果我改变方法使用sub!
, only the first instance will be changed. ,只会更改第一个实例。 I thought the following would work:我认为以下方法可行:
myregex = /(There is text here: )(.+)/.match(input)
hash.each do |k, v|
myregex[2].gsub!("#{k}", "#{v}")
end
But it does not.但事实并非如此。 I need to keep the regex since it is part of a substitution, and modification previously made on the input.我需要保留正则表达式,因为它是替换的一部分,并且之前对输入进行了修改。
What would be the syntax to make the change only inside a specific subgroup in a regex matched to the input?仅在与输入匹配的正则表达式中的特定子组内进行更改的语法是什么?
I think you may be after the following, but please tell me if I'm wrong.我想你可能会关注以下内容,但如果我错了,请告诉我。
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+/
matches strings of characters that contain no whitespace. /\\S+/
匹配不包含空格的字符串。
g = Hash.new { |_,k| k }
creates an empty hash with a default proc that causes g[k]
to return k
if g
does not have a key k
.创建具有一种使得默认PROC一个空散列g[k]
以返回k
如果g
不具有关键k
。
g.merge(h)
returns a hash having the default proc just created with keys and values from h
.返回一个散列,该散列具有刚刚使用h
键和值创建的默认 proc。
See the doc for the form of Hash::new that takes a second argument that is a hash.请参阅Hash::new形式的文档,该形式采用散列形式的第二个参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.