# Parsing with delimeters ---new programmer

**URL:** <https://www.ruby-forum.com/t/parsing-with-delimeters-new-programmer/101021>\
**Category:** Ruby\
**Created:** [August 2, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/parsing-with-delimeters-new-programmer/101021 "2006-08-02T22:19:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [August 2, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/parsing-with-delimeters-new-programmer/101021/1 "2006-08-02T22:19:36Z")

</div>

I’m trying to parse a file that looks like this:

text…  
text…  
numbers…  
blalba bla

and then somewhere in the file i have a table like this:

IDS\_SOMETHIGN\_SOMETHING “giving you cookies in a sec”  
IDX\_SOMETHING\_SOMETHIGN “hi again”  
IDY\_SMOTHIENG\_SOMETHIGN\_SOMTHIGN “HELO world”  
ID\_ONE\_THING “hello how are you”  
ID\_ONETHINGTOO “give me a number”  
ID\_ONEMORE\_THING\_ANDMORE “this are strings”

I’m trying to create a hash table but i’m not sure what’s wrong with my  
delimeters:

class Parser  
table = {}

File.foreach filename do |line|  
next unless line =~ /^ID(\S+) “(.\*?)”/  
table[$1] = $2  
end

end

this doesn’t store anything in the table…any help?  
thanks

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [August 3, 2006, 1:16pm UTC](https://www.ruby-forum.com/t/parsing-with-delimeters-new-programmer/101021/2 "2006-08-03T13:16:48Z")

</div>

Robert wrote:

> wrong with my  
> end  
> Your regular expression does not accommodate more than one space between  
> the key and the value, which all your lines have. Also, if there are  
> leading spaces, you need to accommodate them.

- Mark.

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [August 3, 2006, 1:17pm UTC](https://www.ruby-forum.com/t/parsing-with-delimeters-new-programmer/101021/3 "2006-08-03T13:17:10Z")

</div>

Is your Regexp in multiline mode? (Sorry, I can’t remember if it’s the  
default or not…)

M.T.

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [August 3, 2006, 1:19pm UTC](https://www.ruby-forum.com/t/parsing-with-delimeters-new-programmer/101021/4 "2006-08-03T13:19:10Z")

</div>

> next unless line =~ /^ID(\S+) “(.\*?)”/

You might be wanting to match more than one whitespace in between the  
hash’s  
key and value?  
/^ID(\S+)\s+"(.\*?)"/

-rak-

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [August 3, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/parsing-with-delimeters-new-programmer/101021/5 "2006-08-03T14:23:26Z")

</div>

Robert S. wrote:

> IDX\_SOMETHING\_SOMETHIGN “hi again”  
> IDY\_SMOTHIENG\_SOMETHIGN\_SOMTHIGN “HELO world”  
> ID\_ONE\_THING “hello how are you”  
> ID\_ONETHINGTOO “give me a number”  
> ID\_ONEMORE\_THING\_ANDMORE “this are strings”
> 
> I’m trying to create a hash table but i’m not sure what’s wrong with my  
> delimeters:

Ricardo, on Aug. 1 you posted this:

> ID\_ONETHINGTOO “give me a number”  
> ID\_ONEMORE\_THING\_ANDMORE “this are strings”

Why did you ignore the answers that you were given at that time?  
You should not have started a new thread.

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [August 3, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/parsing-with-delimeters-new-programmer/101021/6 "2006-08-03T14:23:37Z")

</div>

Harry wrote:

> > Why did you ignore the answers that you were given at that time?  
> > You should not have started a new thread.
> 
> I wondered the same thing. But the answers don’t seem to be showing up  
> at ruby-forum. Maybe he is not seeing them.
> 
> Harry

Yes, I’ve sent an email to Andreas about it (the emails not showing up  
on the forum).

-Justin

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [August 3, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/parsing-with-delimeters-new-programmer/101021/7 "2006-08-03T14:23:36Z")

</div>

> Why did you ignore the answers that you were given at that time?  
> You should not have started a new thread.

I wondered the same thing. But the answers don’t seem to be showing up  
at ruby-forum. Maybe he is not seeing them.

Harry

---

<div class="post-metadata">

**Author:** ![ricardo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a6a055/32.png) [@ricardo](https://www.ruby-forum.com/u/ricardo)\
**Post date:** [August 3, 2006, 1:21pm UTC](https://www.ruby-forum.com/t/parsing-with-delimeters-new-programmer/101021/8 "2006-08-03T13:21:16Z")

</div>

Robert S. wrote:

> IDX\_SOMETHING\_SOMETHIGN “hi again”
> 
> File.foreach filename do |line|  
> next unless line =~ /^ID(\S+) “(.\*?)”/  
> table[$1] = $2  
> end
> 
> end
> 
> this doesn’t store anything in the table…any help?  
> thanks

Hi Robert,

Isn’t this the third time you’ve posted the same question?

The problem I see is that you now are expecting ID followed by one or  
more non-whitespace characters, followed by EXACTLY one space, the the  
quotes.

Change

/^ID(\S+) “(.\*?)”/

to

/^ID(\S+)\s+"(.\*?)"/

(I’ve tested this with your sample input and it worked fine, assuming  
there is no space between the beginning of the line and ‘ID…’)

Don’t forget to output the table somewhere in able to see if it’s  
working (your current code has no output).

Hope that helps.

-Justin
