From: Ruwan Budha Date: 2010-09-18T04:50:35+09:00 Subject: Re: ruby regexpresion Jeremy thanks so much for your help......it works. could you please explain further how %r{(\(.*\)).*/(.*)'..(.*)} captures the 3 interested part from the string...... I know you have explained but if you could kindly explain more with an easy way I think it will help me to learn.... thanks again Jeremy Bopp wrote: > On 9/17/2010 1:39 PM, Ruwan Budha wrote: >> >> (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 -- Posted via http://www.ruby-forum.com/.