Hi, 

In message "[ruby-talk:8465] A newbie question (about regexp)"
    on 01/01/02, "Robert Gustavsson" <robertg / swipnet.se> writes:
>I want to change the above line to some code that executes a regex pattern
>for a string and then to be able to extract the number of matches and the
>matching strings. 

Do you mean like the below?

  str> abracadabra
  pat> r.
  abracadabra (2)
    --     --

If it is right, the following realizes it. 

  st = "\033[7m"
  en = "\033[m"
  STDOUT.sync = true

  while TRUE
    print "str> "
    str = gets
    break if not str
    str.chop!

    print "pat> "
    re = gets
    break if not re
    re.chop!

    match = []
    str.gsub!(re){|i|
      match.push i
      "#{st}#{$&}#{en}"
    }
    print "#{str} (#{match.size})\n"
  end

  print "\n"

>I've looked att $~ but I can't get it to work.

String#gsub! may do pattern matching more than two times whereas $~ is
a MatchingData which has infomation only the last matching.  So, we must
keep all matching data by ourselves in this case.   

Hope this helps, 

-- Gotoken