From: Rob Biedenharn Date: 2008-12-04T02:01:47+09:00 Subject: Re: string with \ On Dec 3, 2008, at 10:52 AM, Vrone Ve wrote: > Rob Biedenharn wrote: >> On Dec 3, 2008, at 10:12 AM, Vrone Ve wrote: >> >>> cheers >> irb> string = "abc/ def/ ghi/" >> => "abc/ def/ ghi/" >> irb> string.split(%r{/\s*}) >> => ["abc", "def", "ghi"] >> >> You could use parallel assignment, but I'd question whether you >> really >> wanted three variables rather than a three-element array. >> >> a, d, g = string.split(%r{/\s*}) >> >> >> -Rob >> >> Rob Biedenharn http://agileconsultingllc.com >> Rob@AgileConsultingLLC.com > > thanks Rob, > > My detailed question is regarding a string something like > String "ABC\Name X\Gender " > and I want to scan the string for each of the element(i.e. Name, > Gender) > and whereever Name is written in the string, I want to grab the value > placed before the back slash and store it in a variable. same is the > case with gender and/or with any other attribute i want to be in this > string. > > Cheers Then I presume you want a transformation something like: magic("ABC\\Name X\\Gender") => [["Name", "ABC"], ["Gender", "X"]] or => { "Name" => "ABC", "Gender" => "X" } You can then manipulate the alist or hash (key-value pairs) as you wish. (for the alist, "association list", you can use the Array#assoc method) Depending on where whitespace is permitted in the original string, you might be able to: irb> "ABC\\Name X\\Gender".split(' ').map{|pair| pair.split(/\ \/).reverse } => [["Name", "ABC"], ["Gender", "X"]] -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com