Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 替换字符串中的多个不同单词_Ruby - Fatal编程技术网

Ruby 替换字符串中的多个不同单词

Ruby 替换字符串中的多个不同单词,ruby,Ruby,我在Ruby中有一个字符串: The animal_name is located in the some_place 我如何用鱼来代替动物的名字,用湖来代替一些地方 我用了句子.sub!”animal_name、“fish”对于一个单词非常有效,但我只允许使用两个参数,因此我不能同时更改不同类型的单词 我想做一些类似的事情: 句子.sub!”动物名称“=>”鱼“,”某个地方“=>”湖“ 有什么办法吗 您可以使用gsub提供一个块,在该块中,您可以确定替换每个单词的正确值: sentence

我在Ruby中有一个字符串:

The animal_name is located in the some_place 
我如何用鱼来代替动物的名字,用湖来代替一些地方

我用了句子.sub!”animal_name、“fish”对于一个单词非常有效,但我只允许使用两个参数,因此我不能同时更改不同类型的单词

我想做一些类似的事情:

句子.sub!”动物名称“=>”鱼“,”某个地方“=>”湖“

有什么办法吗

您可以使用gsub提供一个块,在该块中,您可以确定替换每个单词的正确值:

sentence = 'The animal_name is located in the some_place'
mapping = {'animal_name' => 'fish', 'some_place' => 'lake'}
sentence.gsub(/#{mapping.keys.join('|')}/){|w| mapping[w]}
返回位于湖中的鱼。

您可以使用gsub提供一个块,在该块中,您可以确定替换每个单词的正确值:

sentence = 'The animal_name is located in the some_place'
mapping = {'animal_name' => 'fish', 'some_place' => 'lake'}
sentence.gsub(/#{mapping.keys.join('|')}/){|w| mapping[w]}
返回的鱼位于湖中。

那是因为子!可以通过三种方式调用:

sub(pattern, replacement) → new_str
sub(pattern, hash) → new_str
sub(pattern) {|match| block } → new_str 
但是你没有使用这些表格。您只传递散列,因此仍然需要该模式

但是,这不会像您预期的那样起作用,因为sub仅替换模式中的第一个匹配项:

p 'The animal_name is located in the some_place'.gsub(/\b\w+_.*?\b/, 'animal_name' => 'fish', 'some_place' => 'lake')
# "The fish is located in the lake"
从文档:返回str的副本,其中第一次出现 模式替换为第二个参数

所以你可以试着用gsub代替gsub!,但总是通过模式:

p 'The animal_name is located in the some_place'.gsub(/\b\w+_.*?\b/, 'animal_name' => 'fish', 'some_place' => 'lake')
# "The fish is located in the lake"
注意,正则表达式只是一个例子。我只举一个例子就可以了。

那是因为sub!可以通过三种方式调用:

sub(pattern, replacement) → new_str
sub(pattern, hash) → new_str
sub(pattern) {|match| block } → new_str 
但是你没有使用这些表格。您只传递散列,因此仍然需要该模式

但是,这不会像您预期的那样起作用,因为sub仅替换模式中的第一个匹配项:

p 'The animal_name is located in the some_place'.gsub(/\b\w+_.*?\b/, 'animal_name' => 'fish', 'some_place' => 'lake')
# "The fish is located in the lake"
从文档:返回str的副本,其中第一次出现 模式替换为第二个参数

所以你可以试着用gsub代替gsub!,但总是通过模式:

p 'The animal_name is located in the some_place'.gsub(/\b\w+_.*?\b/, 'animal_name' => 'fish', 'some_place' => 'lake')
# "The fish is located in the lake"

注意,正则表达式只是一个例子。我只举一个例子就可以做到这一点。

Regexpunion很好地提供了一个模式:

sentence = 'The animal_name is located in the some_place'
mapping = {'animal_name' => 'fish', 'some_place' => 'lake'}

p sentence.gsub(Regexp.union(mapping.keys), mapping)

Regexpunion很适合提供一种模式:

sentence = 'The animal_name is located in the some_place'
mapping = {'animal_name' => 'fish', 'some_place' => 'lake'}

p sentence.gsub(Regexp.union(mapping.keys), mapping)

我相信@steenslag将gsub与散列一起使用是执行所需替换的正常方式。我相信@steenslag将gsub与散列一起使用是执行所需替换的正常方式;一个变量是map=mapping.tap{map}map.default{u proc=->h,k{k};语句.gsub/\S+/,映射。