Feature #739

Regexp#match with position

Added by Jeremy Stephens about 3 years ago. Updated 9 months ago.

[ruby-core:19785]
Status:Closed Start date:11/11/2008
Priority:Normal Due date:
Assignee:- % Done:

0%

Category:-
Target version:1.9.1 Release Candidate

Description

Regexp#=~ returns the position of the match.  Regexp#match lets you search a string from a certain position.  It would be great if Regexp#match also returned the position along with MatchData.  In the current version (ruby-1.9.1-preview1), rb_reg_match_m already finds the position via reg_match_pos, but it doesn't return it.  It would be trivial to stash the position somewhere in the return value.

I suppose you could either create an attribute reader in MatchData for position, or maybe Regexp#match could return an array of length 2, where the first element is the position, and the second is the MatchData object.

History

Updated by Nobuyoshi Nakada about 3 years ago

Hi,

At Tue, 11 Nov 2008 06:42:08 +0900,
Jeremy Stephens wrote in [ruby-core:19785]:
> I suppose you could either create an attribute reader in
> MatchData for position, or maybe Regexp#match could return an
> array of length 2, where the first element is the position,
> and the second is the MatchData object.

MatchData#begin.

  $ ruby -e 'm=/b/.match("abc"); p m.begin(0)'
  1

-- 
Nobu Nakada

Updated by Jeremy Stephens about 3 years ago

Great! That's what I needed.  Thanks :)

Updated by Koichi Sasada about 3 years ago

  • Status changed from Open to Closed

Also available in: Atom PDF