在gsub或scan中的匹配位置 [英] Matching position in gsub or scan

查看:105
本文介绍了在gsub或scan中的匹配位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用gsubscan时,对于每次匹配,达到匹配位置(=~将返回的索引)的最佳方法是什么?

What is the best way to achieve the matching position (the index that would be returned by =~) for each match when using gsub or scan?

推荐答案

"hello".gsub(/./) { Regexp.last_match.offset(0).first }
 => "01234" 

请参见 Regexp.last_match MatchData .

这篇关于在gsub或scan中的匹配位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆