2007/7/10, Divya Badrinath <dbadrinath / dash.net>:
> Divya Badrinath wrote:
> > 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
>
> i mean i need the 2nd column and the last column.
> >
> > 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.

Your regexp isn't too bad.  With only a little tweaking I get this
which is not too inefficient IMHO:

irb(main):001:0> s = "root     14051 14033  3 08:39 pts/2    00:00:00 /bin/bash"
=> "root     14051 14033  3 08:39 pts/2    00:00:00 /bin/bash"
irb(main):004:0> s =~
/^\S+\s+(\d+)\s+\d+\s+\d+\s+\d+:\d+\s+\S+\s+\d+:\d+:\d+\s+(\S+)/
=> 0
irb(main):005:0> [$1, $2]
=> ["14051", "/bin/bash"]

Or you can do

irb(main):007:0> s =~ /^\S+\s+(\d+)\s+(?:\S+\s+){5}(\S+)/
=> 0
irb(main):008:0> [$1, $2]
=> ["14051", "/bin/bash"]
irb(main):011:0> /^\S+\s+(\d+)\s+(?:\S+\s+){5}(\S+)/.match(s)[1..-1]
=> ["14051", "/bin/bash"]

Kind regards

robert