# String with \\

**URL:** <https://rubytalk.org/t/string-with/50507>\
**Category:** ruby-talk\
**Created:** [3 December 2008 15:12 UTC](https://rubytalk.org/t/string-with/50507 "2008-12-03T15:12:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vrone\_Ve](https://avatars.discourse-cdn.com/v4/letter/v/f08c70/32.png) [@Vrone\_Ve](https://rubytalk.org/u/Vrone_Ve)\
**Post date:** [3 December 2008 15:12 UTC](https://rubytalk.org/t/string-with/50507/1 "2008-12-03T15:12:23Z")

</div>

Hi all

string = "abc/ def/ ghi/"

how to get all three of these dynamic values placed before backslash in  
separate variables.  
any suggestitons??

cheers

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [3 December 2008 15:30 UTC](https://rubytalk.org/t/string-with/50507/2 "2008-12-03T15:30:28Z")

</div>

Vrone Ve wrote:

> Hi all
> 
> string = "abc/ def/ ghi/"
> 
> how to get all three of these dynamic values placed before backslash in  
> separate variables.  
> any suggestitons??

[http://www.ruby-doc.org/docs/ProgrammingRuby/](http://www.ruby-doc.org/docs/ProgrammingRuby/)

Look at String#split, i.e. the split method of the String class

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Craig\_Demyanovich](https://avatars.discourse-cdn.com/v4/letter/c/e47c2d/32.png) [@Craig\_Demyanovich](https://rubytalk.org/u/Craig_Demyanovich)\
**Post date:** [3 December 2008 15:32 UTC](https://rubytalk.org/t/string-with/50507/3 "2008-12-03T15:32:36Z")

</div>

See String#split and String#strip.

Regards,  
Craig

> **···**
>
> On Wed, Dec 3, 2008 at 10:12 AM, Vrone Ve \<vrone@hotmail.co.uk\> wrote:
> 
> > Hi all
> > 
> > string = "abc/ def/ ghi/"
> > 
> > how to get all three of these dynamic values placed before backslash in  
> > separate variables.  
> > any suggestitons??

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [3 December 2008 15:39 UTC](https://rubytalk.org/t/string-with/50507/4 "2008-12-03T15:39:41Z")

</div>

> Hi all
> 
> string = "abc/ def/ ghi/"
> 
> how to get all three of these dynamic values placed before backslash in  
> separate variables.  
> any suggestitons??
> 
> cheers

> string = "abc/ def/ ghi/"

=\> "abc/ def/ ghi/"

> 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](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Dec 3, 2008, at 10:12 AM, Vrone Ve wrote:

---

<div class="post-metadata">

**Author:** ![Vrone\_Ve](https://avatars.discourse-cdn.com/v4/letter/v/f08c70/32.png) [@Vrone\_Ve](https://rubytalk.org/u/Vrone_Ve)\
**Post date:** [3 December 2008 15:52 UTC](https://rubytalk.org/t/string-with/50507/5 "2008-12-03T15:52:30Z")

</div>

Rob Biedenharn wrote:

> > cheers
> 
> \> string = "abc/ def/ ghi/"  
> =\> "abc/ def/ ghi/"  
> \> 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](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

> **···**
>
> > On Dec 3, 2008, at 10:12 AM, Vrone Ve wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [3 December 2008 17:01 UTC](https://rubytalk.org/t/string-with/50507/6 "2008-12-03T17:01:47Z")

</div>

> Rob Biedenharn wrote:
> 
> > > cheers
> > 
> > \> string = "abc/ def/ ghi/"  
> > =\> "abc/ def/ ghi/"  
> > \> 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](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:

> "ABC\\Name X\\Gender".split(' ').map{|pair| pair.split(/\

\/).reverse }  
=\> [["Name", "ABC"], ["Gender", "X"]]

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Dec 3, 2008, at 10:52 AM, Vrone Ve wrote:
> 
> > > On Dec 3, 2008, at 10:12 AM, Vrone Ve wrote:

---

<div class="post-metadata">

**Author:** ![Vrone\_Ve](https://avatars.discourse-cdn.com/v4/letter/v/f08c70/32.png) [@Vrone\_Ve](https://rubytalk.org/u/Vrone_Ve)\
**Post date:** [3 December 2008 17:03 UTC](https://rubytalk.org/t/string-with/50507/7 "2008-12-03T17:03:23Z")

</div>

Vrone Ve wrote:

> Rob Biedenharn wrote:
> 
> > > cheers
> > 
> > \> string = "abc/ def/ ghi/"  
> > =\> "abc/ def/ ghi/"  
> > \> 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](http://agileconsultingllc.com)  
> > Rob@AgileConsultingLLC.com

thanks Rob, and all for your response

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.

Your help required.  
cheers

> **···**
>
> > > On Dec 3, 2008, at 10:12 AM, Vrone Ve wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![David\_Wright](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@David\_Wright](https://rubytalk.org/u/David_Wright)\
**Post date:** [3 December 2008 17:28 UTC](https://rubytalk.org/t/string-with/50507/8 "2008-12-03T17:28:14Z")

</div>

Vrone Ve wrote:

> Vrone Ve wrote:
> 
> > Rob Biedenharn wrote:
> > 
> > > > cheers
> > > 
> > > \> string = "abc/ def/ ghi/"  
> > > =\> "abc/ def/ ghi/"  
> > > \> 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](http://agileconsultingllc.com)  
> > > Rob@AgileConsultingLLC.com
> 
> thanks Rob, and all for your response
> 
> 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.
> 
> Your help required.  
> cheers

rather then split, I believe you want scan.

irb(main):032:0\> str = 'ABC\Name X\Gender Z\Person '  
=\> "ABC\\Name X\\Gender Z\\Person "  
irb(main):033:0\> str.scan(/(.\*?)\\\w+\s?/)  
=\> [["ABC"], ["X"], ["Z"]]

irb(main):034:0\> t = str.scan(/(.\*?)\\\w+\s?/)  
=\> [["ABC"], ["X"], ["Z"]]  
irb(main):035:0\> t  
=\> [["ABC"], ["X"], ["Z"]]

> **···**
>
> > > > On Dec 3, 2008, at 10:12 AM, Vrone Ve wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Vrone\_Ve](https://avatars.discourse-cdn.com/v4/letter/v/f08c70/32.png) [@Vrone\_Ve](https://rubytalk.org/u/Vrone_Ve)\
**Post date:** [3 December 2008 17:49 UTC](https://rubytalk.org/t/string-with/50507/9 "2008-12-03T17:49:09Z")

</div>

What if There is some optional value which may appear in strings  
oftenly, and i want to check against its attribute value. or in other  
words its not necesary to have Name with its value every time in the  
string, and i want to have some checks on it ( e.g if Name attribute is  
there then grab its value before the back slash, then moving along all  
other attributes and chk there occurance take their value)

thanks again

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [3 December 2008 20:20 UTC](https://rubytalk.org/t/string-with/50507/10 "2008-12-03T20:20:57Z")

</div>

> What if There is some optional value which may appear in strings  
> oftenly, and i want to check against its attribute value. or in other  
> words its not necesary to have Name with its value every time in the  
> string, and i want to have some checks on it ( e.g if Name attribute is  
> there then grab its value before the back slash, then moving along all  
> other attributes and chk there occurance take their value)
> 
> thanks again

> "ABC\\Name X\\Gender Z\\Person ".scan(/([^\\]+)\\(\w+)\s?/)

=\> [["ABC", "Name"], ["X", "Gender"], ["Z", "Person"]]

> alist = \_

=\> [["ABC", "Name"], ["X", "Gender"], ["Z", "Person"]]

> alist.rassoc('Name')

=\> ["ABC", "Name"]

> alist.rassoc('Person')

=\> ["Z", "Person"]

> alist.rassoc('Optional')

=\> nil

If you still can't figure it out, either hire someone or at least post your actual problem with some real data. Even a little code fragment that you think is close, but perhaps misbehaving in a few cases. Showing those cases as tests would be even better. 😉

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Dec 3, 2008, at 12:49 PM, Vrone Ve wrote:
