From: Eliezer Croitoru Date: 2012-08-16T04:46:50+09:00 Subject: Re: string processing On 8/15/2012 10:00 PM, Fosiul Alam wrote: > hi > I am very new to ruby > what would be the syntax ?? > > i get output like this > > uk-xxxxxxx-Z15A14 > Adding uk-xxxxx-Z15A14 (uk-xxxx-Z15A14) as "Generic SNMP-enabled Host" > using SNMP v1 with community "public" > Success - new device-id: (26) > > i will be able to parse the line by > > cmd each > > cmd.each do |item| > if ( item =~ /\((\d)\)/ ) > getnumber = item > end > end > > will it work ?? > this output should be a string and is not enumerable so you can use each but you can use "each_line" to inspect each line but "getnumber = item" will not extract the number. and instead of iterating over each line you can use one regex on the whole string like: cmd =~ /\((\d+)\)/ you must use "\d+" to match one or more numbers the =~ will give bad results on a whole string and you better use the scan method. cmd.scan /\((\d+)\)/ this will return array of strings array so this: (cmd.scan /\((\d+)\)/)[0][0] will give you the result with a one liner. and i just seen that Robert gave you a nice solution. Regards, Eliezer -- Eliezer Croitoru https://www1.ngtech.co.il IT consulting for Nonprofit organizations eliezer ngtech.co.il