You know you can get values out of an array with the [] operator.
Well you can get strings out of strings that same way, and it works
with regexes!
string[/[0-9]+/] will return the first match of 1 or more numbers
Here's the magic use [ ] inside of a regular expression to create your
own groups. Individual characters in there are included in the group,
and ranges may be included using the -. so a-b is
abcdefghijklmnopqrstuvwxyz.
The + afterwards means 1 or more times.
What if you want _exactly 5 consecutive numbers? use the {}
string[/[0-9]{5}/]
ranges also work here
string[/[0-9]{3-5}/] would match 3, 4 or 5 digit numbers
and
string[/[a-z\/]+$/] will match a text string containing the forward
slash at the end. The $ is a special char to represent the end of a
line, and since / is a special char itself, it needed to be escaped
with a \.
BUT it could even be easier.
the [] groups, can be negative!
/[^a]*/ would match any string that did not have an a in it
/[^ ]*/ would match any string that did not have a space in it...soo
string[/[^ ]+$/] would be a good way to get the last bit.
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.
> 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.
cols = string.split
sec, last = cols.values_at(1, -1)
Very interesting James, I seem to be rather extreme and
sec, last = string.split.values_at(1, -1)
might be a tad to long for one line in your style, however Ruby syntax
just supports this marvelous syntax
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 Negri wrote:
> sorry for being OT since I'm not going to talk about ruby or regexp
>
> If the string you're parsing is an output from the ps command you can
> simplify your life using the -o option that prints only the fields you
> need.
>
> I.E. in gnu Linux
>
> ps -ao pid,command
>
> just outputs pid and command columns.
> Be careful since the command column can contain spaces.
>
> Paolo
i saw that too. But i can not use all the options in a ps command where
i am using.
i am limited to using ps -aef
i need to take care of fetching the stuff i need using from this result.
On 7/10/07, James Edward Gray II <james@grayproductions.net> wrote:
> On Jul 10, 2007, at 3:25 PM, Divya Badrinath wrote:
>
> > 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.
>
> cols = string.split
> sec, last = cols.values_at(1, -1)
Very interesting James, I seem to be rather extreme and
sec, last = string.split.values_at(1, -1)
might be a tad to long for one line in your style, however Ruby syntax
just supports this marvelous syntax
Very interesting James, I seem to be rather extreme and
sec, last = string.split.values_at(1, -1)
might be a tad to long for one line in your style, however Ruby syntax
just supports this marvelous syntax
sec, last = string.split.
values_at(1, -1)
Robert
cmd = string[/\s(\S+)$/, 1]
doesnt fetch me anything:)
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.
sec, last = string.split.values_at(1, -1)
doesnt work for the same reason
i need everything after 00.00.00 till the end
i.e., /bin/bash -x -s
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]
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.