From: Jeremy Bopp Date: 2010-09-18T04:20:51+09:00 Subject: Re: ruby regexpresion On 9/17/2010 1:39 PM, Ruwan Budha wrote: > (21:15:32:873) SMART>> Starting Process '/abs/nc/qwcy/xyz'. pid 11560 > > > I want to parse the following few things from the above string > > (21:15:32:873) > xyz > pid 11560 > > and output as below, > > (21:15:32:873), xyz, pid 11560 > > > I am stuck and I would appreciate if someone could give me some help. I > am newbie to ruby....thanks The following example will work for your singular example, but it's likely that it won't work for your most general case since it assumes a fair bit about the contents of the lines to parse. You'll need to read up on regular expressions in general if you need to edit this. Fortunately, there are plenty of decent references online: http://www.regular-expressions.info/ruby.html http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm http://www.zenspider.com/Languages/Ruby/QuickRef.html#12 or just google for ruby regexp. :-) Example: # Assume str holds the string you want to parse. str = .... str =~ %r{(\(.*\)).*/(.*)'..(.*)} puts [$1, $2, $3].join(', ') This regexp uses the parenthesis around the first field of interest as the first marker and captures the text within and including the parenthesis. Then it chews up everything following that until the last /, which is the second marker. It captures the following text until the ', the fourth marker. Finally it throws away the next 2 characters and captures the rest of the string. It has captured 3 matches which become available in the special variables $1, $2, and $3 based on the order in which they were matched in the string. For convenience, those are put into an array, joined with ', ', and then printed out. HTH. :-) -Jeremy