如何找出ruby中每场比赛的起点

时间:2013-06-19 08:02:52

标签: ruby-on-rails ruby ruby-on-rails-3 string hashtag

说,我有一个跟随字符串

string = "#Sachin is Indian cricketer. #Tendulkar is right hand batsman. #Sachin has been honoured with the Padma Vibhushan award "

我希望o / p为

"#Sachin|0|7;#Tendulkar|29|10;#Sachinn|63|7;"

我试过了

 new_string = ""
 string.scan(/#\S+/).each{|match| new_string+="#{match}|#{string.index(match)}|#{match.length};"  }

给了我

 "#Sachin|0|7;#Tendulkar|29|10;#Sachin|0|7;" 

那么我将如何获得每个子字符串的起始索引?

3 个答案:

答案 0 :(得分:3)

这实际上是一项非常重要的任务,并且在SO的其他问题上已经讨论了很多。这是最常见的解决方案:

string = "#Sachin is Indian cricketer. #Tendulkar is right hand batsman. #Sachin has been honoured with the Padma Vibhushan award "
new_string = string.to_enum(:scan,/#\S+/i).inject(''){|s,m| s + "#{m}|#{$`.size}|#{m.length};"}

答案 1 :(得分:1)

基于这个帖子How do I get the match data for all occurrences of a Ruby regular expression in a string?只是简单的例子:

string = "#Sachin is Indian cricketer. #Tendulkar is right hand batsman. #Sachin has been honoured with the Padma Vibhushan award "
new_string = ""
string
  .to_enum(:scan, /#\S+/)
  .each do |wrd|
    m = Regexp.last_match
    new_string += "#{wrd}|#{m.offset(0)[0]}|#{wrd.length};"
  end
p new_string

答案 2 :(得分:1)

这是使用扫描的那个:

offset = 0
string.scan(/(#\S*)([^#]*)/).map{|m| v = "#{m[0]}|#{offset}|#{m[0].length};"; offset += m.join.length; v}.join
#=> "#Sachin|0|7;#Tendulkar|29|10;#Sachin|63|7;"