Help: Efficient regular expression

R

Robert Dober

On 7/10/07 said:
cmd = string[/\s(\S+)$/, 1]
doesnt fetch me anything:)
No idea that was not my code :)
program=string.split.last
what if
string = "root 14051 14033 3 08:39 pts/2 00:00:00 /bin/bash -x
-s"
it fetches only -s for me.
As simple as possible, but not simpler. Now we are in the simpler case :)
But see below
program=string[/[a-z\/]+$/]
the command column mauy start with character. i dont want to limit it in
my regexp. it has to be generic.

with all your comments, i tried
pid = run_process[/\s(\d+)/, 1]
cmd = run_process[/:\d+:\d+\s(\S.*)\s$/, 1]
Does not work the :\d+ stuff might be a parameter of the program :(
is there any other way?
Yep :) counting fields after all
x = split
y= x[1]
z = x[7..-1].join(" ")

There might still be a pitfall though in case of some old processes
where you will have to analyse if the date takes one or two fields if
memory serves.

HTH
Robert
 
R

Robert Klemme

2007/7/10 said:
i mean i need the 2nd column and the last column.

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
 
P

Paolo Negri

You can process the ps output using other commands.

ps -aef|tr -s ' '|cut -d ' ' -f2,8-

This will just print your second column the 8th and anything that
comes after. At this point your regexp only need to split the string
at the first space.

Paolo
 
M

Martin DeMello

You can process the ps output using other commands.

ps -aef|tr -s ' '|cut -d ' ' -f2,8-

This will just print your second column the 8th and anything that
comes after. At this point your regexp only need to split the string
at the first space.

You're in trouble if any of your fields have spaces, though the
specific case of ps -aef looks safe enough.

martin
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top