how to find the position of each match within a string

Discussion in 'Ruby' started by Li Chen, Jan 24, 2009.

  1. Li Chen

    Li Chen Guest

    Hi all,

    I have a small script to find a match. If a match is found I need to
    print its position within the string. I try the String#index but it only
    returns the first match. Any idea?

    Thanks,

    Li

    C:\Documents and Settings\chen73\My Documents\Ruby\bin>irb
    irb(main):001:0> "ello-hello".scan('e') do |match|
    irb(main):002:1* p "ello-hello".index(match)
    irb(main):003:1> end
    0
    0
    => "ello-hello"
    irb(main):004:0>
    --
    Posted via http://www.ruby-forum.com/.
     
    Li Chen, Jan 24, 2009
    #1
    1. Advertising

  2. > You can examine the $~ global MatchData object, in a couple of ways:
    >
    > irb(main):003:0> "ello-hello".scan('e') do |match|
    > irb(main):004:1* p $~.pre_match.size
    > irb(main):005:1> end
    > 0
    > 6


    Interesting. Does that mean that String#scan() converts a String
    argument to RegExp?
    --
    Posted via http://www.ruby-forum.com/.
     
    Albert Schlef, Jan 24, 2009
    #2
    1. Advertising

  3. On Sun, Jan 25, 2009 at 12:31 AM, Albert Schlef <> wr=
    ote:
    >> You can examine the $~ global MatchData object, in a couple of ways:
    >>
    >> irb(main):003:0> "ello-hello".scan('e') do |match|
    >> irb(main):004:1* p $~.pre_match.size
    >> irb(main):005:1> end
    >> 0
    >> 6

    >
    > Interesting. Does that mean that String#scan() converts a String
    > argument to RegExp?


    Yes it does, even if it search fixed-size string.


    --=20
    Pozdrawiam

    Rados=B3aw Bu=B3at
    http://radarek.jogger.pl - m=F3j blog
     
    Rados³aw Bu³at, Jan 24, 2009
    #3
  4. Radosław Bułat wrote:
    >>> You can examine the $~ global MatchData object, in a couple of ways:
    >>>
    >>> irb(main):003:0> "ello-hello".scan('e') do |match|
    >>> irb(main):004:1* p $~.pre_match.size
    >>> irb(main):005:1> end
    >>> 0
    >>> 6

    >>
    >> Interesting. Does that mean that String#scan() converts a String
    >> argument to RegExp?

    >
    > Yes it does, even if it search fixed-size string.


    I'm shocked. Why does it do this? (this conversion)

    And doesn't this mean that #scan is less efficient than it could have
    been? Isn't matching regexps slower than matching plain strings?
    --
    Posted via http://www.ruby-forum.com/.
     
    Albert Schlef, Jan 25, 2009
    #4
  5. Li Chen

    Li Chen Guest

    David A. Black wrote:
    > You can examine the $~ global MatchData object, in a couple of ways:
    >
    > irb(main):003:0> "ello-hello".scan('e') do |match|
    > irb(main):004:1* p $~.pre_match.size
    > irb(main):005:1> end
    > 0
    > 6
    > => "ello-hello"
    > irb(main):006:0> "ello-hello".scan('e') do |match|
    > irb(main):007:1* p $~.offset(0)[0]
    > irb(main):008:1> end
    > 0
    > 6
    > => "ello-hello


    Hi David,

    Thanks.

    I go back and read the chapter about MatchData. And it helps me a lot.

    Li
    --
    Posted via http://www.ruby-forum.com/.
     
    Li Chen, Jan 25, 2009
    #5
    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. Daniel Bohner

    Position of a String within a String

    Daniel Bohner, Feb 26, 2004, in forum: C++
    Replies:
    3
    Views:
    552
    John Harrison
    Feb 26, 2004
  2. Replies:
    11
    Views:
    1,228
    John S
    Jun 13, 2009
  3. Michael W. Ryder
    Replies:
    6
    Views:
    112
    Michael W. Ryder
    Jan 21, 2008
  4. Old Echo
    Replies:
    1
    Views:
    182
    Adam Shelly
    Sep 4, 2008
  5. Victor
    Replies:
    3
    Views:
    136
    Victor
    Sep 12, 2003
Loading...

Share This Page