Regexp#match(str, *offset*)

Discussion in 'Ruby' started by trans. (T. Onoma), Sep 23, 2004.

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

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

    But not with Regexp itself?

    T.
     
    trans. (T. Onoma), Sep 23, 2004
    #1
    1. Advertising

  2. 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
     
    James Edward Gray II, Sep 23, 2004
    #2
    1. Advertising

  3. trans.  (T. Onoma)

    Guest

    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
     
    , Sep 30, 2004
    #3
  4. 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
     
    Simon Strandgaard, Sep 30, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lance Riedel

    Translated Offset to Source Offset

    Lance Riedel, Oct 14, 2003, in forum: XML
    Replies:
    2
    Views:
    524
    Patrick TJ McPhee
    Oct 15, 2003
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    542
    Mikel Lindsaar
    Mar 31, 2008
  3. Old Echo
    Replies:
    1
    Views:
    200
    Adam Shelly
    Sep 4, 2008
  4. Uldis  Bojars
    Replies:
    2
    Views:
    213
    Janwillem Borleffs
    Dec 17, 2006
  5. Roy Smith
    Replies:
    4
    Views:
    313
    Roy Smith
    Jan 27, 2013
Loading...

Share This Page