3

=~使用gsubor时,为每个匹配项实现匹配位置(将返回的索引)的最佳方法是什么scan

4

2 回答 2

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

请参阅Regexp.last_matchMatchData

于 2012-03-04T11:25:15.370 回答
1

我从一个不同的方向来解决这个问题,并且无法提出一个体面的解决方案(即,可理解的、可维护的)来使用 gsub 或 scan (String 类的两个内置方法)来做到这一点。所以我问“为什么这样做?......”并寻找更自然的替代品(感谢 Nash 指出大方向!):

#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-

# capture_all_matches.rb
#
# Copyright © 2015 Lorin Ricker <Lorin@RickerNet.us>
#
# This program is free software, under the terms and conditions of the
# GNU General Public License published by the Free Software Foundation.
# See the file 'gpl' distributed within this project directory tree.

# This sample code demonstrates three ways to capture *all* of the offsets
# [begin,end,length] data for *all* matches scanned in a source string.

# Of course, each/any of the below examples could be turned into a class method
# of String &/or Regexp -- One wonders why these are not part of the built-in
# classes/methods?...

# An example source string (any will do):
s = "The fox hides in the box full of sox eating lox."
#       4^                  25^   31^

# Use the literal pattern /f/ as an example --
# there are three "f"s in the sample source string;
# see indexes above...
p = /f/

# 1. Just report an array of the begin (start) position of each match:
mpos = []
m = i = 0
m = p.match( s, i ) { |k| j = k.begin(0); i = j + 1; mpos << j } while m
p mpos   # => [4, 25, 31]

# 2. Make an array containing elements [begin,end] of matched substrings:
mpos = []
m = i = 0
m = p.match( s, i ) { |k| j = k.offset(0); i = j[0] + 1; mpos << j } while m
p mpos   # => [[4, 5], [25, 26], [31, 32]]

# 3. Make an array containing elements [begin,end,length] of matched substrings:
mpos = []
m = i = 0
m = p.match( s, i ) { |k| j = k.offset(0); i = j[0] + 1;
                          j << j[1] - j[0]; mpos << j    } while m
p mpos   # => [[4, 5, 1], [25, 26, 1], [31, 32, 1]]

通过将以上内容粘贴到 Ruby 源文件(例如 capture_all_matches.rb)中进行演示,然后:

$ ruby capture_all_matches.rb

请注意,RegExp 匹配方法可以(重新)从源字符串中的任意偏移量开始,因此只需捕获“最后匹配的偏移量”并从那里迭代即可。

只需要每场比赛的起始偏移量,或者开始和结束,还是开始结束长度?滚动您自己的结果数组。

希望这可以帮助。

于 2015-10-19T20:26:56.713 回答