# Regexp#match(str, \*offset\*)

**URL:** <https://rubytalk.org/t/regexp-match-str-offset/13753>\
**Category:** ruby-talk\
**Created:** [23 September 2004 09:16 UTC](https://rubytalk.org/t/regexp-match-str-offset/13753 "2004-09-23T09:16:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![T\_Onoma](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@T\_Onoma](https://rubytalk.org/u/T_Onoma)\
**Post date:** [23 September 2004 09:16 UTC](https://rubytalk.org/t/regexp-match-str-offset/13753/1 "2004-09-23T09:16:50Z")

</div>

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

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

But not with Regexp itself?

T.

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [23 September 2004 13:34 UTC](https://rubytalk.org/t/regexp-match-str-offset/13753/2 "2004-09-23T13:34:14Z")

</div>

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:
> > 
> > &nbsp;&nbsp;str.index(/re/,offset)  
> > &nbsp;&nbsp;$~.begin  
> > &nbsp;&nbsp;$~.end
> > 
> > But not with Regexp itself?

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [30 September 2004 04:01 UTC](https://rubytalk.org/t/regexp-match-str-offset/13753/3 "2004-09-30T04:01:13Z")

</div>

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:
> 
> &nbsp;&nbsp;str.index(/re/,offset)  
> &nbsp;&nbsp;$~.begin  
> &nbsp;&nbsp;$~.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

---

<div class="post-metadata">

**Author:** ![Simon\_Strandgaard1](https://avatars.discourse-cdn.com/v4/letter/s/e47c2d/32.png) [@Simon\_Strandgaard1](https://rubytalk.org/u/Simon_Strandgaard1)\
**Post date:** [30 September 2004 16:23 UTC](https://rubytalk.org/t/regexp-match-str-offset/13753/4 "2004-09-30T16:23:10Z")

</div>

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
