How to find out the starting point for each match in ruby

走远了吗. 提交于 2019-12-03 09:11:54

This is actually quite a non-trivial task, and has been discussed quite a bit in other questions on SO. This is the most common solution:

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

Here's one that uses scan:

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

Based on this thread How do I get the match data for all occurrences of a Ruby regular expression in a string? just quick example:

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!