# String parsing?

**URL:** <https://rubytalk.org/t/string-parsing/21884>\
**Category:** ruby-talk\
**Created:** [25 October 2005 19:32 UTC](https://rubytalk.org/t/string-parsing/21884 "2005-10-25T19:32:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Zanis](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@Michael\_Zanis](https://rubytalk.org/u/Michael_Zanis)\
**Post date:** [25 October 2005 19:32 UTC](https://rubytalk.org/t/string-parsing/21884/1 "2005-10-25T19:32:58Z")

</div>

Hi,

I am new to ruby but have an okay background in perl. I am trying to parse  
a string into an array.

string = (foo:0.05,bar:0.115);

I would like the array to look like this:

Array[0] = (  
Array[1] = foo  
Array[2] = 0.05  
Array[3] = ,  
Array[4] = bar  
Array[5] = 0.115  
Array[6] = )  
Array[7] = ;

I have a rather clunky perl script for this, but in learning ruby I thought  
I might see if there is an elegant way to parse this string using tokens.  
Thank you for any help you might have.

-M

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [25 October 2005 19:42 UTC](https://rubytalk.org/t/string-parsing/21884/2 "2005-10-25T19:42:34Z")

</div>

> Hi,
> 
> I am new to ruby but have an okay background in perl. I am trying to parse  
> a string into an array.
> 
> string = (foo:0.05,bar:0.115);
> 
> I would like the array to look like this:
> 
> Array[0] = (  
> Array[1] = foo  
> Array[2] = 0.05  
> Array[3] = ,  
> Array[4] = bar  
> Array[5] = 0.115  
> Array[6] = )  
> Array[7] = ;

See if this helps:

\>\> "(foo:0.05,bar:0.115);".scan(/[a-z]+|\d+\.\d+|[(,);]/)  
=\> ["(", "foo", "0.05", ",", "bar", "0.115", ")", ";"]

James Edward Gray II

> **···**
>
> On Oct 25, 2005, at 2:32 PM, Michael Zanis wrote:

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [25 October 2005 20:00 UTC](https://rubytalk.org/t/string-parsing/21884/3 "2005-10-25T20:00:02Z")

</div>

This may be an overkill, but take a look at my grammar project:

[http://rubyforge.org/projects/grammar/](http://rubyforge.org/projects/grammar/)

You can build a lexer (what you want) and/or a parser with ruby  
syntax that looks kind of like BNF.

But keep in mind the above is only the first release. I'm  
building a ruby parser/lexer right now and doing this is  
revealing various holes and inconsistencies in the API. The  
basics of the API will remain unchanged though: "+" create a  
sequence (from 2 grammars) and "|" will create an alternation  
(from 2 grammars). The basic idea is that you start with base  
grammar objects and you build more complex grammar objects  
using various methods to combine or morph them.

> **···**
>
> --- Michael Zanis \<mzanis@purdue.edu\> wrote:
> 
> > Hi,
> > 
> > I am new to ruby but have an okay background in perl. I am  
> > trying to parse  
> > a string into an array.
> > 
> > string = (foo:0.05,bar:0.115);
> > 
> > I would like the array to look like this:
> > 
> > Array[0] = (  
> > Array[1] = foo  
> > Array[2] = 0.05  
> > Array[3] = ,  
> > Array[4] = bar  
> > Array[5] = 0.115  
> > Array[6] = )  
> > Array[7] = ;
> > 
> > I have a rather clunky perl script for this, but in learning  
> > ruby I thought  
> > I might see if there is an elegant way to parse this string  
> > using tokens.  
> > Thank you for any help you might have.
> > 
> > -M
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Yahoo! Mail - PC Magazine Editors' Choice 2005
> 
> > **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
> >
> > It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...
