如何获取字符串中所有出现的模式的索引 [英] How to get indexes of all occurrences of a pattern in a string

查看:74
本文介绍了如何获取字符串中所有出现的模式的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string = "Jack and Jill went up the hill to fetch a pail of water. Jack fell down and broke his crown. And Jill came tumbling after. "
d = string.match(/(jack|jill)/i) # -> MatchData "Jill" 1:"Jill"
d.size # -> 1

这只匹配第一次出现的情况.
string.scan部分地完成了这项工作,但它没有告诉您有关匹配模式索引的任何信息.

This only match the first occurrence it seems.
string.scan does the job partially but it doesn't tell anything about the index of the matched pattern.

如何获取模式的所有匹配实例及其索引(位置)的列表?

How do i get a list of all the matched instances of the pattern and their indices (positions)?

推荐答案

您可以使用.scan$`全局变量,这表示最后一次成功匹配左侧的字符串,但它在通常的.scan中不起作用,因此您需要此 hack (从此答案):

You can use .scan and $` global variable, which means The string to the left of the last successful match, but it doesn't work inside usual .scan, so you need this hack (stolen from this answer):

string = "Jack and Jill went up the hill to fetch a pail of water. Jack fell down and broke his crown. And Jill came tumbling after. "  
string.to_enum(:scan, /(jack|jill)/i).map do |m,|
  p [$`.size, m]
end

输出:

[0, "Jack"]
[9, "Jill"]
[57, "Jack"]
[97, "Jill"]

UPD:

请注意lookbehind的行为–您获得的是真正匹配的部分的索引,而不是 look 的索引:

Note the behaviour of lookbehind – you get the index of the really matched part, not the look one:

irb> "ab".to_enum(:scan, /ab/     ).map{ |m,| [$`.size, $~.begin(0), m] }
=> [[0, 0, "ab"]]
irb> "ab".to_enum(:scan, /(?<=a)b/).map{ |m,| [$`.size, $~.begin(0), m] }
=> [[1, 1, "b"]]

这篇关于如何获取字符串中所有出现的模式的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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