删除以'#'开头的单词,前n个单词除外

时间:2015-07-10 14:47:10

标签: ruby regex

给定一个字符串和一个整数n,我想删除所有以&#34开头的单词;#"在第n次发生之后。

例如,如果n = 2且字符串为:

" #Chamonix 法国的山脉以滑雪,高山攀登,跳垒, #Paragliding ,原始冒险和 #Home而闻名第一届冬季奥运会。它是冒险家和摄影师的天堂。在这里看到,长期曝光的 #Druis ,其中一个引人注目的"

然后结果应该是:

" #Chamonix 法国的山脉以滑雪,高山攀登,跳垒, #Paragliding ,原始冒险和第一届冬季奥运会而闻名。它是冒险家和摄影师的天堂。在这里看到,长期曝光,很多引人注目的"

保留了前两个#前缀单词#Chamonix#Paragliding,但删除了剩余的#-prefixed单词#Home#Druis。< / p>

4 个答案:

答案 0 :(得分:5)

如果要在第n次出现后删除以"#"开头的所有单词,则:

string.gsub(/#\w+/).with_index(1){|s, i| i > n ? "" : s}

答案 1 :(得分:0)

这应该有效(简单易读):

saved = 0
str.split.map do |word|
  ret = word
  if word[0] == '#'
    if saved == n
      ret = ''
    else
      saved += 1
    end
  end
  ret
end.join

答案 2 :(得分:0)

您可以使用String#gsub和正则表达式执行此操作,如下所示。

str = "The #quick, brown #dog #jumped over #the #lazy fox"
n = 2 # Number of words beginning with '#' to skip

r = /
    (?:     # start a non-capture group
    \#\w+   # match a word beginning with '#'
    [^#]+   # match all characters other than '#'
    )       # end the non-capture group
    {#{n}}? # do that n times, lazily
    \K      # disregard everything matched so far
    \#\w+   # match a word beginning with '#'
    /x      # extended mode

str.gsub(r,'')
  #=> "The #quick, brown #dog  over   fox"

答案 3 :(得分:-1)

如果我的问题是正确的,您希望删除n之后的所有哈希标记字词。你可以这样做:

str = "The mountains of #Chamonix France are famous for skiing, alpine climbing, base-jumping, #Paragliding, raw adventure and #Home of the first Winter Olympics. It's an adventurers and photographers paradise. Seen here, a long exposure of the #Druis, one of many striking"
n = 2

str.gsub(/#{str.scan(/#\w+/)[n..-1].join('|')}/,'')
# => "The mountains of #Chamonix France are famous for skiing, alpine climbing, base-jumping, #Paragliding, raw adventure and  of the first Winter Olympics. It's an adventurers and photographers paradise. Seen here, a long exposure of the , one of many striking"

<强>解释

首先我们得到所有哈希标记词:

str.scan(/#\w+/)
# => ["#Chamonix", "#Paragliding", "#Home", "#Druis"]

现在我们只关心第二个指数之后的单词,即

str.scan(/#\w+/)[n..-1]  # n is 2
# => ["#Home", "#Druis"] 

现在让我们创建一个正则表达式模式,在String中查找这些单词。这里:

/str.scan(/#\w+/)[n..-1].join('|')/
# /#Home|#Druis/

最后使用''将其替换为gsub。把所有事情放在一起,我们得到:

str.gsub(/#{str.scan(/#\w+/)[n..-1].join('|')}/,'')