Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regexp#match(str, *offset*)

Reply
Thread Tools

Regexp#match(str, *offset*)

 
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      09-23-2004
Curious, where's the offset? I can do this with String#index:

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

But not with Regexp itself?

T.


 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      09-23-2004
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?


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



 
Reply With Quote
 
 
 
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      09-30-2004
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


 
Reply With Quote
 
Simon Strandgaard
Guest
Posts: n/a
 
      09-30-2004
On Thursday 30 September 2004 06:01, 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


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

Thanks.

--
Simon Strandgaard


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57