Ruby 仅替换作为散列传递的多个正则表达式的第一次出现

Ruby replacing only the first occurrence of multiple regexes passed as a hash

我有一段文字,只想 Ruby sub 这个词的第一个正则表达式匹配。如果我只需要匹配一个字符串就好了,但是我将多个正则表达式传递到我的子程序中:

regex = Regexp.new(["Lebron James", "Chris Paul"].join("|"))
names_hash = {"Lebron James" => "**Lebron James**", "Chris Paul" => "**Chris Paul**"}

str = "of the best players left in the playoffs, Lebron James is the most experienced player left in the field and probably in all of the league. Chris Paul has played in many playoff games but has never been to a conference final. Lebron James on the other hand, has been to seven straight NBA finals."

如果我 运行 str.gsub(regex, names_hash),勒布朗詹姆斯和克里斯保罗的所有实例都会被替换为:

"of the best players left in the playoffs, Lebron James is the most experienced player left in the field and probably in all of the league. Chris Paul has played in many playoff games but has never been to a conference final. Lebron James on the other hand, has been to seven straight NBA finals."

如果我 运行 str.sub(regex, names_hash)(sub 而不是 gsub),我只会得到 Lebron James 的第一次出现,而不是 Chris Paul:

"of the best players left in the playoffs, Lebron James is the most experienced player left in the field and probably in all of the league. Chris Paul has played in many playoff games but has never been to a conference final. Lebron James on the other hand, has been to seven straight NBA finals."

我的问题:

我如何设置我所拥有的,以便我可以替换 Lebron James 和 Chris Paul 的第一个实例,但不能替换 Lebron James 的第二个引用?我的预期结果:

"of the best players left in the playoffs, Lebron James is the most experienced player left in the field and probably in all of the league. Chris Paul has played in many playoff games but has never been to a conference final. Lebron James on the other hand, has been to seven straight NBA finals."

一种选择是按顺序为每个名称分别调用 sub

或者,您可以使用 gsub 的块形式来跟踪您已经突出显示的名称:

names_seen = []
regex = Regexp.union(["Lebron James", "Chris Paul"])

str = ..
str.gsub(regex) do |name|
  if names_seen.include?(name)
    name # not the first; replace with itself
  else
    names_seen << name # remember
    "**#{name}**" # or use `names_hash[name]` if needed
  end
end

怎么样:

regex = Regexp.new(["Lebron James", "Chris Paul"].join("|"))
names_hash = {"Lebron James" => "**Lebron James**", "Chris Paul" => "**Chris Paul**"}
str = "of the best players left in the playoffs, Lebron James is the most experienced player left in the field and probably in all of the league. Chris Paul has played in many playoff games but has never been to a conference final. Lebron James on the other hand, has been to seven straight NBA finals."


str.gsub(regex) { |name| names_hash.delete(name) || name }

这将从 names_hash 中读取,仅用于第一个替换;在那之后,gsub 将"default" 保持不变。

请注意,此方法会改变原始 names_hash - 因此如果稍后需要变量,您可能需要事先 dup 它。

尽管 is good, I want to show you a different way of solving your issue. My solution involves calling String#sub 有多少次你就有多少名字。

str = 'of the best players left in the playoffs, Lebron James is the most experienced player left in the field and probably in all of the league. Chris Paul has played in many playoff games but has never been to a conference final. Lebron James on the other hand, has been to seven straight NBA finals.'
names = ['Lebron James', 'Chris Paul']

原回答

replacements = names.map { |name| "**#{name}**" }
replacements = names.zip(replacements)

replacements.inject(str) { |str, args| str.sub(*args) }

一样,#map / #zip 可能有点矫枉过正。您可以 运行 以下内容代替:

names.inject(str) { |str, name| str.sub(name, "**#{name}**") }

returns

"of the best players left in the playoffs, Lebron James is the most experienced player left in the field and probably in all of the league. Chris Paul has played in many playoff games but has never been to a conference final. Lebron James on the other hand, has been to seven straight NBA finals."

引用