如何删除 ruby 中定义字符串之间的字符串?

How do I remove strings in ruby that are between defined strings?

我的目标是能够定义不同的子字符串集,可以在不删除其他字符串的情况下将其删除。接受更好的想法。

我现在拥有的是:

@outbound_text = " XTEST this is hidden XTEST hey there, what's up! XTEST this is also hidden XTEST but then I just keep writing here "

我尝试了以下但意识到它正在删除 hey there, what's up

if ENV['ENVIRONMENT'] == 'test'

  # this will allow the XTEST string to come through

else # for production and development, remove the XTEST

  unless @outbound_text.gsub!(/XTEST(.*)XTEST/  , '').nil?
    @outbound_text.strip!
  end

  logger.debug "remove XTEST: #{@outbound_text}"
end

打开不同的字符串书尾我需要删除的内容(但隐藏的子字符串的数量会有所不同,因此它们只能是开头和结尾)。

我认为开放 - 虽然其中有一些被解析,所以开放使用 Nokogiri 删除隐藏的标签。我需要花一些时间来尝试,但在尝试之前想知道是否有简单的 gsub。

重复non-greedy:

@outbound_text.gsub(/XTEST(.*?)XTEST/  , '').strip
  # => "hey there, what's up!  but then I just keep writing here"