ruby regex: match and get position(s) of

后端 未结 3 489
暖寄归人
暖寄归人 2020-12-03 01:00

I\'d like to match a regex and get the position in the string of the match

For example,

\"AustinTexasDallasTexas\".match_with_posn /(Texas)/
<         


        
3条回答
  •  感情败类
    2020-12-03 01:45

    "AustinTexasDallasTexas".gsub(/Texas/).map { Regexp.last_match.begin(0) }
      #=> [6, 17]
    

提交回复
热议问题