1

说,我有一个以下字符串

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;" 

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

4

3 回答 3

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};"}
于 2013-06-19T08:45:17.283 回答
1

基于此线程如何获取字符串中所有出现的 Ruby 正则表达式的匹配数据?只是简单的例子:

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
于 2013-06-19T08:46:23.000 回答
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;"
于 2013-06-19T08:51:49.377 回答