Regexp#match(str, *offset*)

Curious, where's the offset? I can do this with String#index:

  str.index(/re/,offset)
  $~.begin
  $~.end

But not with Regexp itself?

T.

Regexps already include that ability natively. For example if you wanted to find the first space character after a 5 character offset, you could use:

/^.{5,}? /

Using Regexps to handle offsets can be significantly more flexible too. For example, here's a pattern to match the last space that occurs not more than 81 characters into the string (perhaps useful for wrapping):

/^.{0,80} /

Hope that helps.

James Edward Gray II

···

On Sep 23, 2004, at 4:16 AM, trans. (T. Onoma) wrote:

Curious, where's the offset? I can do this with String#index:

  str.index(/re/,offset)
  $~.begin
  $~.end

But not with Regexp itself?

Hi,

At Thu, 23 Sep 2004 18:16:50 +0900,
trans. (T. Onoma) wrote in [ruby-talk:113491]:

Curious, where's the offset? I can do this with String#index:

  str.index(/re/,offset)
  $~.begin
  $~.end

But not with Regexp itself?

$ ruby19 -ve 'p(/\s/.match(" abc def", 0).begin(0))'
ruby 1.9.0 (2004-09-29) [i686-linux]
1
$ ruby19 -ve 'p(/\s/.match(" abc def", 1).begin(0))'
ruby 1.9.0 (2004-09-29) [i686-linux]
4

···

--
Nobu Nakada

Wonderful Nobu.. This is a feature I always have needed without knowing it.

Thanks.

···

On Thursday 30 September 2004 06:01, nobu.nokada@softhome.net wrote:

At Thu, 23 Sep 2004 18:16:50 +0900,
trans. (T. Onoma) wrote in [ruby-talk:113491]:
> Curious, where's the offset? I can do this with String#index:
>
> str.index(/re/,offset)
> $~.begin
> $~.end
>
> But not with Regexp itself?

$ ruby19 -ve 'p(/\s/.match(" abc def", 0).begin(0))'
ruby 1.9.0 (2004-09-29) [i686-linux]
1
$ ruby19 -ve 'p(/\s/.match(" abc def", 1).begin(0))'
ruby 1.9.0 (2004-09-29) [i686-linux]
4

--
Simon Strandgaard