# Simple gsub question \\' \\\` what?

**URL:** <https://www.ruby-forum.com/t/simple-gsub-question-what/90683>\
**Category:** Ruby\
**Created:** [March 30, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/simple-gsub-question-what/90683 "2007-03-30T23:19:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [March 30, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/simple-gsub-question-what/90683/1 "2007-03-30T23:19:31Z")

</div>

Sorry for the simple question, I just can’t figure this out after a  
couple hours…

I need to parse a string:

string = column-1:block-0,block-2,block-1,block-3

and I need to extract everything before the : (colon) into one variable  
(should end up like this:  
string1 = column-1

and then the rest of the string (block-0,block-2,block-1,block3) into an  
array…

How can I use gsub to extract everything before the colon?

I’ve looked at the documentation and just can’t understand how to use  
the ` or ' syntax… I want the two new strings to be:

string1 = column-1  
string2 = [“block-0”, “block-2”, “block-1”, “block-3”]

I’m thinking it’s something like this:

string.gsub(/:/, “`”)

really, I just don’t get it…

the documentation that I’m looking at is here:  
[http://dev.rubycentral.com/ref/ref\_c\_string.html](http://dev.rubycentral.com/ref/ref_c_string.html)

thanks a bunch!  
-Dustin

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [March 30, 2007, 11:29pm UTC](https://www.ruby-forum.com/t/simple-gsub-question-what/90683/2 "2007-03-30T23:29:01Z")

</div>

Alle venerdÃ¬ 30 marzo 2007, Dustin A. ha scritto:

> -Dustin

I don’t think you need gsub here. gsub is used to replace parts of a  
string  
with other characters. What you need is a regexp. This should work:

string.match( /^([^:]_):(._)$/ )  
string1=$1  
string2=$2.split(’,’)

The regexp will put anything from the beginning of the string until the  
first : (excluded) in the global variable $1 and anything after the : in  
the  
variable $2. You can then convert $2 to an array of string using the  
String#split method.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [March 30, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/simple-gsub-question-what/90683/3 "2007-03-30T23:37:03Z")

</div>

Hello,

you could just use split:

> > split = string.split(’:’)  
> > string1 = split[0]  
> > string2 = split[1].split(’,’)

– Kristoffer

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [March 30, 2007, 11:41pm UTC](https://www.ruby-forum.com/t/simple-gsub-question-what/90683/4 "2007-03-30T23:41:03Z")

</div>

Dustin A. wrote:

> and then the rest of the string (block-0,block-2,block-1,block3) into an  
> array…

I’d use two calls to split:

irb(main):001:0\> string = ‘column-1:block-0,block-2,block-1,block-3’  
=\> “column-1:block-0,block-2,block-1,block-3”  
irb(main):002:0\> string1, s = string.split(’:’)  
=\> [“column-1”, “block-0,block-2,block-1,block-3”]  
irb(main):003:0\> string1  
=\> “column-1”  
irb(main):004:0\> string2 = s.split(’,’)  
=\> [“block-0”, “block-2”, “block-1”, “block-3”]  
irb(main):005:0\>

You could probably turn that into a one-liner but that’s just golfing.

---

<div class="post-metadata">

**Author:** ![dkanders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@dkanders](https://www.ruby-forum.com/u/dkanders)\
**Post date:** [March 31, 2007, 12:32am UTC](https://www.ruby-forum.com/t/simple-gsub-question-what/90683/5 "2007-03-31T00:32:09Z")

</div>

On Mar 30, 2007, at 5:39 PM, Timothy H. wrote:

> > (should end up like this:  
> > irb(main):002:0\> string1, s = string.split(‘:’)  
> > =\> [“column-1”, “block-0,block-2,block-1,block-3”]  
> > irb(main):003:0\> string1  
> > =\> “column-1”  
> > irb(main):004:0\> string2 = s.split(‘,’)  
> > =\> [“block-0”, “block-2”, “block-1”, “block-3”]  
> > irb(main):005:0\>
> 
> You could probably turn that into a one-liner but that’s just golfing.

Here’s a bit of “live” code that does the same thing (plus .to\_f on  
the elements before turning them into a Vector, but you can knock  
that part out yourself)

def add line  
id, vals = line.split(/:\s\*/, 2)  
@labels \<\< id  
@data \<\< Vector[\*(vals.split(‘,’).map {|v| v.to\_f})]  
end

Note that the second arg (2) to split is important if a ‘:’ can occur  
anywhere in your block-N parts. Of course, you just need vals.split  
(‘,’)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
