From: "Florian Aßmann" Date: 2007-07-11T05:48:25+09:00 Subject: Re: Help: Efficient regular expression Divya Badrinath schrieb: > string = "root 14051 14033 3 08:39 pts/2 00:00:00 /bin/bash" > > i need to fetch 14051 and /bin/bash from the string > > can someone help me to write an efficient regular expression for that. > > i am a beginner, i wrote > string =~ /(\d+)\s+(\d+)\s+\d+\s+\d+:\d+\s+.*\s+\d+:\d+:\d+\s+(.*)\s/ > > i know this is not the efficient way of doing it. > > Please help. > talking about efficient, I was just curious... #!/usr/bin/env ruby -w # # Created by Florian Aßmann on 2007-07-10. # Copyright (c) 2007. All rights reserved. string = "root 14051 14033 3 08:39 pts/2 00:00:00 /bin/bash" require 'profiler' puts <<-EOS pid = string[/\s(\d+)/, 1] cmd = string[/\s(\S+)$/, 1] EOS Profiler__::start_profile 10000.times do pid = string[/\s(\d+)/, 1] cmd = string[/\s(\S+)$/, 1] end Profiler__::stop_profile Profiler__::print_profile STDOUT puts <<-EOS cols = string.split sec, last = cols.values_at(1, -1) EOS Profiler__::start_profile 10000.times do cols = string.split sec, last = cols.values_at(1, -1) end Profiler__::stop_profile Profiler__::print_profile STDOUT puts <<-EOS number = string.split[1] program = string.split.last EOS Profiler__::start_profile 10000.times do number = string.split[1] program = string.split.last end Profiler__::stop_profile Profiler__::print_profile STDOUT *grin* Florian