From: Simone D'Amico Date: 2009-12-31T04:46:30+09:00 Subject: Re: How to parse this file Thank you very much. Il giorno 26/dic/2009, alle ore 18.20, Hassan Schroeder ha scritto: > On Sat, Dec 26, 2009 at 9:03 AM, Rajinder Yadav wrote: > >> here is the basic code, output followed by a basic explanation > >> data=line.match /\"(\w+)\"\s+\"(\w+)\"/ > > ?? That won't remotely work, unless the "Question" and "Answer" > are both single words, which seems unlikely :-) > > Try something like line.chomp.match /\"([^"]+)\"\s+\"([^"]+)\"/ > >>> '"What is the temperature?" "Pretty cold"'.match /\"(\w+)\"\s+\"(\w+)\"/ > => nil >>> '"What is the temperature?" "Pretty cold"'.match /\"([^"]+)\"\s+\"([^"]+)\"/ > => # is the temperature?" 2:"Pretty cold"> >>> result = '"What is the temperature?" "Pretty cold"'.match /\"([^"]+)\"\s+\"([^"]+)\"/ > => # is the temperature?" 2:"Pretty cold"> >>> result[1] > => "What is the temperature?" >>> result[2] > => "Pretty cold" >>> > > -- > Hassan Schroeder ------------------------ hassan.schroeder@gmail.com > twitter: @hassan > Simone D'Amico *nix powered sim@me.com